Skip to content

Commit

Permalink
Allow tooltip as value for descriptionDisplay
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Tubach committed Oct 14, 2024
1 parent ca6550f commit 116bf36
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 16 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# JSON Forms for Drupal

JSON Forms for Drupal is an implementation of the [JSON Forms](
https://jsonforms.io/) specification for Drupal.

## Additional features and keywords

In this implementation there are some custom features and keywords not
specified in standard JSON Forms. (TODO: Not all additional possibilities are
described here, yet.)

### Description display

The Keyword `descriptionDisplay` in Control options allows to specify the
display of the description. Possible options:

* `after`
* `before`
* `invisible`
* `tooltip`

The first three options are standard options available for the
`#description_display` in Drupal.

The option `tooltip` leads to an additional CSS class on the description
element: `json-forms-description-tooltip`. This can be used to process it
with another module to display the description as tooltip.

With the module [Form Tips](https://www.drupal.org/project/formtips) it can
be achieved with this CSS selector:

```css
:not(.json-forms-description-tooltip)
```

## Limitations

Some things cannot be done with (standard) Drupal forms, e.g.
[Rules](https://jsonforms.io/docs/uischema/rules/) cannot completely be mapped
to [conditional form fields](https://www.drupal.org/docs/drupal-apis/form-api/conditional-form-fields).

TODO: Describe all limitations.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"php-http/discovery": true,
"phpstan/extension-installer": true
"php-http/discovery": false,
"phpstan/extension-installer": false,
"tbachert/spi": false
}
},
"require": {
Expand Down
23 changes: 23 additions & 0 deletions json_forms.module
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
<?php
declare(strict_types=1);

use Drupal\Core\Template\Attribute;

function _json_forms_add_tooltip_css_class(array &$variables): void {
if ('tooltip' === ($variables['element']['#_json_forms_description_display'] ?? NULL)) {
/** @var \Drupal\Core\Template\Attribute|null $descriptionAttributes */
$descriptionAttributes = $variables['description']['attributes'] ?? NULL;
if (NULL !== $descriptionAttributes) {
// Add the CSS class "json-forms-description-tooltip" to the description
// element. This can be used to process the element with another module.
$descriptionAttributes->merge(new Attribute(['class' => ['json-forms-description-tooltip']]));
}
}
}

function json_forms_preprocess_form_element(array &$variables): void {
_json_forms_add_tooltip_css_class($variables);
}

function json_forms_preprocess_fieldset(array &$variables): void {
_json_forms_add_tooltip_css_class($variables);
}
17 changes: 3 additions & 14 deletions src/Form/Control/Util/BasicFormPropertiesFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\json_forms\Form\Control\Callbacks\RecalculateCallback;
use Drupal\json_forms\Form\Control\Rule\StatesArrayFactory;
use Drupal\json_forms\Form\Util\DescriptionDisplayUtil;
use Drupal\json_forms\JsonForms\Definition\Control\ControlDefinition;

final class BasicFormPropertiesFactory {
Expand All @@ -48,20 +49,8 @@ public static function createBasicProperties(ControlDefinition $definition): arr
$form['#description'] = $definition->getDescription();
}

$form['#description_attributes']['class'][] = 'foo';
switch ($definition->getOptionsValue('descriptionDisplay')) {
case 'after':
$form['#description_display'] = 'after';
break;

case 'before':
$form['#description_display'] = 'before';
break;

case 'invisible':
$form['#description_display'] = 'invisible';
break;
}
// @phpstan-ignore argument.type
DescriptionDisplayUtil::handleDescriptionDisplay($form, $definition->getOptionsValue('descriptionDisplay'));

if (NULL !== $definition->getOptionsValue('placeholder')) {
$form['#attributes']['placeholder'] = $definition->getOptionsValue('placeholder');
Expand Down
4 changes: 4 additions & 0 deletions src/Form/Layout/AbstractLayoutArrayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Drupal\json_forms\Form\AbstractConcreteFormArrayFactory;
use Drupal\json_forms\Form\Control\Rule\StatesArrayFactoryInterface;
use Drupal\json_forms\Form\FormArrayFactoryInterface;
use Drupal\json_forms\Form\Util\DescriptionDisplayUtil;
use Drupal\json_forms\JsonForms\Definition\Control\ControlDefinition;
use Drupal\json_forms\JsonForms\Definition\DefinitionInterface;
use Drupal\json_forms\JsonForms\Definition\Layout\LayoutDefinition;
Expand All @@ -50,6 +51,9 @@ public function createFormArray(
/** @var \Drupal\json_forms\JsonForms\Definition\Layout\LayoutDefinition $definition */
$form = $this->createBasicFormArray($definition);

// @phpstan-ignore argument.type
DescriptionDisplayUtil::handleDescriptionDisplay($form, $definition->getOptionsValue('descriptionDisplay'));

if (NULL !== $definition->getRule()) {
$form['#states'] = $this->statesArrayFactory->createStatesArray($definition->getRule());
}
Expand Down
50 changes: 50 additions & 0 deletions src/Form/Util/DescriptionDisplayUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* Copyright (C) 2024 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace Drupal\json_forms\Form\Util;

final class DescriptionDisplayUtil {

/**
* @phpstan-param array<string, mixed> $form
*/
public static function handleDescriptionDisplay(array &$form, ?string $descriptionDisplay): void {
if (NULL !== $descriptionDisplay) {
// See module hooks.
$form['#_json_forms_description_display'] = $descriptionDisplay;

switch ($descriptionDisplay) {
case 'after':
$form['#description_display'] = 'after';
break;

case 'before':
$form['#description_display'] = 'before';
break;

case 'invisible':
$form['#description_display'] = 'invisible';
break;
}
}
}

}

0 comments on commit 116bf36

Please sign in to comment.