Compatibility with field group

Created on 5 May 2023, about 1 year ago
Updated 30 May 2024, 27 days ago

Problem

I try to use extra field with field group. I have no problem in displaying extra fields in groups when displaying the entity.
But in the create or edit form, extra field displays outside his group,

It have works with 1.1 version and the patch that add this functionality, but not with 2.x version.

Steps to reproduce

Install field group, extra field and extra field exemple.
Add a group (html element or fieldgroup) in the form display of any content type and move the exemple markup inside.
Try to create a content of this type. The extra field is outside.
And is it is the only element of the group, the group is not display.

✨ Feature request
Status

Needs work

Version

2.3

Component

Code

Created by

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

Comments & Activities

  • Issue created by @marieAscomedia
  • πŸ‡ͺπŸ‡ΈSpain dcraig91

    Same problem here

  • Status changed to Needs work 9 months ago
  • πŸ‡³πŸ‡±Netherlands jaapjan

    Which version of the patch did you use before? In our site we used https://www.drupal.org/files/issues/2019-01-17/extra_field-using_in_form... β†’ . Here you can see (in ExtraFieldFormManager::entityView) that the plugin element was always returned inside a new form field of type container with a widget. Like so:

    $element = $plugin->formView($form, $form_state);
    if (!empty($element)) {
      $form[$field_name] = [
         '#weight' => $extra_field['weight'],
         '#type' => 'container',
         'widget' => $element,
       ];
    }
    

    But in the new 2.x version the element is printed directly. See FormManager.php::entityFormAlter, line 133:

    $form[$fieldName] = $plugin->formElement($form, $formState);
    

    Going back to your example extra field plugin (ExampleMarkup):

    Current code is:

      /**
       * {@inheritdoc}
       */
      public function formElement(array &$form, FormStateInterface $form_state) {
    
        $element['markup'] = ['#markup' => '<p>Hello world 1</p>'];
    
        $element['container'] = [
          '#type' => 'container',
          '#markup' => '<p>Hello world 2</p>',
        ];
    
        $element['item'] = [
          '#type' => 'item',
          '#title' => 'Greeting',
          '#markup' => '<p>Hello world 3</p>',
          '#description' => "A common line of example output.",
        ];
    
        return $element;
      }
    

    It will fix the problem if you change this to:

    
      /**
       * {@inheritdoc}
       */
      public function formElement(array &$form, FormStateInterface $form_state) {
    
        $element['markup'] = ['#markup' => '<p>Hello world 1</p>'];
    
        $element['container'] = [
          '#type' => 'container',
          '#markup' => '<p>Hello world 2</p>',
        ];
    
        $element['item'] = [
          '#type' => 'item',
          '#title' => 'Greeting',
          '#markup' => '<p>Hello world 3</p>',
          '#description' => "A common line of example output.",
        ];
    
        return [
          '#type' => 'container',
          'widget' => $element,
        ];
      }
    

    I think this means that we have two options:

    1. Developer is responsible for adding a type to your extra_field element. We should probably add this in the examples if we go down this route.
    2. Restore the way the element is added to the form, like in patch 24 or add a fallback when there is no #type provided we add type 'container' to the render array. This may have some impact on existing installations using the 2.x branch of this module.

    Curious to hear what the maintainers think of this.

  • It seem that I use this version of the patch :
    https://www.drupal.org/files/issues/2019-07-31/extra_field-using_in_form... β†’

    I will try your option with the container and let you know the result.
    Thank you.

  • Your solution is working.
    Thank you.

  • πŸ‡ͺπŸ‡ΈSpain pcambra Spain, πŸ‡ͺπŸ‡Ί
  • πŸ‡ͺπŸ‡ΈSpain pcambra Spain, πŸ‡ͺπŸ‡Ί

    This is probably more a documentation/example than anything that the module needs to do, maybe we could add a simple element with field_group example?

Production build 0.69.0 2024