[1.0.0-alpha3] Field formatters compatibility with Layout Builder

Created on 27 August 2024, 5 months ago
Updated 18 September 2024, 4 months ago

Problem/Motivation

A few changes are needed for compatibility with Layout Builder

IconFormatter needs to cover empty strings values in icon_settings :

-    $icon_settings = $this->getSetting('icon_settings') ?? [];
+    $icon_settings = $this->getSetting('icon_settings') ?: [];

IconType needs to implement ::generateSampleValue()

Proposed resolution

Do the changes.

Also, rename the module label in ui_icons_field.info.yml

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇫🇷France pdureau Paris

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @pdureau
  • Status changed to Needs work 5 months ago
  • 🇫🇷France pdureau Paris

    Work done, but let's do more tests before MR

  • Assigned to mogtofu33
  • 🇫🇷France pdureau Paris

    Jean,

    There may be something wrong with the custom IconFormatter::validateSettings() method:

      public function validateSettings(array $element, FormStateInterface $form_state, &$complete_form): void {
        $values = $form_state->getValues();
        $name = $this->fieldDefinition->getName();
    
        $settings = $values['fields'][$name]['settings_edit_form']['settings'] ?? [];
        unset($settings['icon_settings']);
    
        // @todo do we need configuration validation of plugin form?
        $form_state->setValueForElement($element, $settings);
      }

    $values['fields'][$name]['settings_edit_form']['settings'] seems to mimic the form structure of manage display, so the formatter settings are not saved in layout builder.

    If I remove the call to validateSettings:

        $elements['icon_settings'] = [
          '#type' => 'hidden',
          '#element_validate' => [
            [$this, 'validateSettings'],
          ],
        ];

    the settings are not saved in neither Manage Display nor Layotu Builder.

    I don't know which change to make. Do you have an idea?

    Field formatter must be tesetd with Manage Dispaly, Layout Builder, and Views.

  • First commit to issue fork.
  • 🇫🇷France mogtofu33

    Fixed for Icon formatter in Layout builder, but can not make it work for Link with Icon as sample values do not populate the icon.

    And for now I didn't find hot to do it.

  • Status changed to Fixed 5 months ago
  • 🇫🇷France mogtofu33

    Link formatter issue created.

  • 🇫🇷France pdureau Paris

    Is it possible to avoid doing such condition which is limited to "Manage display" and "Layout builder":?

        // 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'];
        }
    

    I am afraid the plugin will not work with Views and any other config entity from Drupal contrib leveraging this plugin type

  • Status changed to Fixed 5 months ago
  • Issue was unassigned.
Production build 0.71.5 2024