Verified Commit d842b8d2 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #2991772 by nuez, smustgrave, ravi.shankar, sanfair: Field layouts...

Issue #2991772 by nuez, smustgrave, ravi.shankar, sanfair: Field layouts doen't seem to work for embedded forms

(cherry picked from commit 3324f140)
parent fc4d59e3
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -107,9 +107,14 @@ public function buildForm(array &$build, EntityDisplayWithLayoutInterface $displ
        // moving the field in the form structure. If a #group is already set,
        // do not overwrite it.
        if (isset($regions[$field['region']]) && !isset($build[$name]['#group'])) {
          if (!empty($build['#parents'])) {
            $build[$name]['#group'] = implode('][', array_merge($build['#parents'], ['_field_layout', $field['region']]));
          }
          else {
            $build[$name]['#group'] = $field['region'];
          }
        }
      }
      // Ensure this will not conflict with any existing array elements by
      // prefixing with an underscore.
      $build['_field_layout'] = $display->getLayout()->build($regions);
+7 −0
Original line number Diff line number Diff line
@@ -5,3 +5,10 @@ entity.entity_test.test_view_mode:
    _title: 'Test test view mode'
  requirements:
    _entity_access: 'entity_test.view'
field_layout_test.embedded_form:
  path: '/field-layout-embedded-form'
  defaults:
    _form: 'Drupal\field_layout_test\Form\EmbeddedForm'
    _title: 'Embedded entity form'
  requirements:
    _permission: 'access administration pages'
+55 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\field_layout_test\Form;

use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field_layout\FieldLayoutBuilder;

/**
 * Provides the EmbeddedForm class.
 *
 * @package Drupal\field_layout_test\Form
 */
class EmbeddedForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'field_layout_test_embedded_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['foo'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Wrapper'),
      '#tree' => TRUE,
      '#parents' => ['foo'],
    ];
    $entity = EntityTest::load(1);
    if ($entity) {
      if ($entity) {
        $display = EntityFormDisplay::collectRenderDisplay($entity, 'default');
        $subform_state = SubformState::createForSubform($form['foo'], $form, $form_state);
        $display->buildForm($entity, $form['foo'], $subform_state);
        \Drupal::classResolver(FieldLayoutBuilder::class)->buildForm($form['foo'], $display, $subform_state);
      }
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Do nothing.
  }

}
+5 −0
Original line number Diff line number Diff line
@@ -127,6 +127,11 @@ public function testEntityForm() {
    $this->assertSession()->elementExists('css', '.layout__region--second .field--name-field-test-text');
    $this->assertFieldInRegion('field_test_text[0][value]', 'second');

    // Tests if this layout works in an embedded context.
    $this->drupalGet('/field-layout-embedded-form');
    $this->assertSession()->elementExists('css', '.layout__region--second .field--name-field-test-text');
    $this->assertFieldInRegion('foo[field_test_text][0][value]', 'second');

    // Move the field to the second region without tabledrag.
    $this->drupalGet('entity_test/structure/entity_test/form-display');
    $this->getSession()->getPage()->pressButton('Show row weights');