Skip to content
Snippets Groups Projects
Commit 22084ef3 authored by Pierre Dureau's avatar Pierre Dureau Committed by Jean Valverde
Browse files

Issue #3470447 by pdureau, mogtofu33: Field formatters compatibility with Layout Builder

parent e03dd5a2
No related branches found
No related tags found
1 merge request!13Issue #3470447 by pdureau: Field formatters compatibility with Layout Builder
Pipeline #266792 passed
......@@ -113,7 +113,7 @@ class IconFormatter extends FormatterBase implements ContainerFactoryPluginInter
public function settingsForm(array $form, FormStateInterface $form_state): array {
$elements = parent::settingsForm($form, $form_state);
$icon_settings = $this->getSetting('icon_settings') ?? [];
$icon_settings = $this->getSetting('icon_settings') ?: [];
$this->pluginManagerIconPack->getExtractorPluginForms($elements, $form_state, $icon_settings, [], TRUE);
......@@ -156,7 +156,16 @@ class IconFormatter extends FormatterBase implements ContainerFactoryPluginInter
$values = $form_state->getValues();
$name = $this->fieldDefinition->getName();
$settings = $values['fields'][$name]['settings_edit_form']['settings'] ?? [];
$settings = [];
// Field UI "MAnage display" page.
if (isset($values['fields'][$name]['settings_edit_form']['settings'])) {
$settings = $values['fields'][$name]['settings_edit_form']['settings'];
}
// Layout builder UI.
elseif (isset($values['settings']['formatter']['settings'])) {
$settings = $values['settings']['formatter']['settings'];
}
unset($settings['icon_settings']);
// @todo do we need configuration validation of plugin form?
......
......@@ -143,7 +143,7 @@ class IconLinkFormatter extends LinkFormatter {
'#attributes' => ['class' => ['description']],
];
$icon_settings = $this->getSetting('icon_settings') ?? [];
$icon_settings = $this->getSetting('icon_settings') ?: [];
$this->pluginManagerIconPack->getExtractorPluginForms($elements, $form_state, $icon_settings, [], TRUE);
......@@ -192,7 +192,16 @@ class IconLinkFormatter extends LinkFormatter {
$values = $form_state->getValues();
$name = $this->fieldDefinition->getName();
$settings = $values['fields'][$name]['settings_edit_form']['settings'] ?? [];
$settings = [];
// Field UI "MAnage display" page.
if (isset($values['fields'][$name]['settings_edit_form']['settings'])) {
$settings = $values['fields'][$name]['settings_edit_form']['settings'];
}
// Layout builder UI.
elseif (isset($values['settings']['formatter']['settings'])) {
$settings = $values['settings']['formatter']['settings'];
}
// Clean unwanted values from link.
foreach (['icon_settings', 'trim_length', 'icon_display', 'url_only', 'url_plain', 'rel', 'target'] as $key) {
unset($settings[$key]);
......
......@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Drupal\ui_icons_field\Plugin\Field\FieldType;
use Drupal\Core\Field\Attribute\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
......@@ -15,11 +16,11 @@ use Drupal\Core\TypedData\MapDataDefinition;
* Plugin implementation of the 'ui_icon' field type.
*/
#[FieldType(
id: "ui_icon",
label: new TranslatableMarkup("UI Icon"),
description: new TranslatableMarkup("Reference an Icon from icon pack definition."),
default_widget: "icon_widget",
default_formatter: "icon_formatter",
id: 'ui_icon',
label: new TranslatableMarkup('UI Icon'),
description: new TranslatableMarkup('Reference an Icon from icon pack definition.'),
default_widget: 'icon_widget',
default_formatter: 'icon_formatter',
list_class: IconFieldItemList::class,
)]
class IconType extends FieldItemBase {
......@@ -71,4 +72,18 @@ class IconType extends FieldItemBase {
return $value === NULL || $value === '';
}
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$manager = \Drupal::service('plugin.manager.ui_icons_pack');
$icons = $manager->getIcons();
if (empty($icons)) {
return [];
}
return [
'target_id' => array_rand($icons),
];
}
}
name: 'UI Icons'
name: 'UI Icons Fields'
type: module
description: 'Field type Icon and field type Link integration.'
package: Field types
......
......@@ -26,13 +26,13 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* @internal
*/
#[Filter(
id: "icon_embed",
title: new TranslatableMarkup("Embed icon"),
description: new TranslatableMarkup("Embeds icon items using a custom tag, <code>&lt;drupal-icon&gt;</code>."),
id: 'icon_embed',
title: new TranslatableMarkup('Embed icon'),
description: new TranslatableMarkup('Embeds icon items using a custom tag, <code>&lt;drupal-icon&gt;</code>.'),
type: FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
weight: 100,
settings: [
"allowed_icon_pack" => [],
'allowed_icon_pack' => [],
],
)]
class IconEmbed extends FilterBase implements ContainerFactoryPluginInterface {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment