You are here

public function FieldLayoutBuilder::buildForm in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/field_layout/src/FieldLayoutBuilder.php \Drupal\field_layout\FieldLayoutBuilder::buildForm()
  2. 9 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\\RenderElement::processGroup';
    $fill['#pre_render'][] = '\\Drupal\\Core\\Render\\Element\\RenderElement::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'])) {
        $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);
  }
}