function FieldLayoutBuilder::buildForm

Same name and namespace in other branches
  1. 9 core/modules/field_layout/src/FieldLayoutBuilder.php \Drupal\field_layout\FieldLayoutBuilder::buildForm()
  2. 8.9.x core/modules/field_layout/src/FieldLayoutBuilder.php \Drupal\field_layout\FieldLayoutBuilder::buildForm()
  3. 11.x core/modules/field_layout/src/FieldLayoutBuilder.php \Drupal\field_layout\FieldLayoutBuilder::buildForm()

Applies the layout to an entity form.

Parameters

array $build: A renderable array representing the entity content or form.

\Drupal\field_layout\Display\EntityDisplayWithLayoutInterface $display: The entity display holding the display options configured for the entity components.

File

core/modules/field_layout/src/FieldLayoutBuilder.php, line 95

Class

FieldLayoutBuilder
Builds a field layout.

Namespace

Drupal\field_layout

Code

public function buildForm(array &$build, EntityDisplayWithLayoutInterface $display) {
  $layout_definition = $this->layoutPluginManager
    ->getDefinition($display->getLayoutId(), FALSE);
  if ($layout_definition && ($fields = $this->getFields($build, $display, 'form'))) {
    $fill = [];
    $fill['#process'][] = '\\Drupal\\Core\\Render\\Element\\RenderElementBase::processGroup';
    $fill['#pre_render'][] = '\\Drupal\\Core\\Render\\Element\\RenderElementBase::preRenderGroup';
    // Add the regions to the $build in the correct order.
    $regions = array_fill_keys($layout_definition->getRegionNames(), $fill);
    foreach ($fields as $name => $field) {
      // As this is a form, #group can be used to relocate the fields. This
      // avoids breaking hook_form_alter() implementations by not actually
      // 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);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.