Compatibility with field group

Created on 5 May 2023, over 1 year 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

Active

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 about 1 year 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 Asturies
  • πŸ‡ͺπŸ‡ΈSpain pcambra Asturies

    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?

  • πŸ‡ͺπŸ‡ΈSpain enchufe Spain

    Thank you #3 ✨ Compatibility with field group Needs work ! Your documentation worked for me out of the box.

  • πŸ‡ΊπŸ‡ΈUnited States mlncn Minneapolis, MN, USA

    We have a module built on Extra Field Plus (and friends) - https://www.drupal.org/project/create_referencing_content β†’ - that seems to be running into this for entity view display, not only the form as described here, with Field Group 3.x. Is there any similar special markup as described here for the widget that that needs to be part of the formatter for it to work as expected?

    I'm pretty confused about this issue too… definitely needs either better documentation across the Extra Field ecosystem or if Extra Field can non-intrusively ensure pseudofields always work with Field Group module, that's ideal?

Production build 0.71.5 2024