-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow
tooltip
as value for descriptionDisplay
- Loading branch information
Dominic Tubach
committed
Oct 14, 2024
1 parent
ca6550f
commit 116bf36
Showing
6 changed files
with
125 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
||
} |