You are here

public function FieldLayoutBuilder::buildView in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field_layout/src/FieldLayoutBuilder.php \Drupal\field_layout\FieldLayoutBuilder::buildView()

Applies the layout to an entity build.

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 63

Class

FieldLayoutBuilder
Builds a field layout.

Namespace

Drupal\field_layout

Code

public function buildView(array &$build, EntityDisplayWithLayoutInterface $display) {
  $layout_definition = $this->layoutPluginManager
    ->getDefinition($display
    ->getLayoutId(), FALSE);
  if ($layout_definition && ($fields = $this
    ->getFields($build, $display, 'view'))) {

    // Add the regions to the $build in the correct order.
    $regions = array_fill_keys($layout_definition
      ->getRegionNames(), []);
    foreach ($fields as $name => $field) {

      // If the region is controlled by the layout, move the field from the
      // top-level of $build into a region-specific section. Custom regions
      // could be set by other code at run-time; these should be ignored.
      // @todo Ideally the array structure would remain unchanged, see
      //   https://www.drupal.org/node/2846393.
      if (isset($regions[$field['region']])) {
        $regions[$field['region']][$name] = $build[$name];
        unset($build[$name]);
      }
    }

    // Ensure this will not conflict with any existing array elements by
    // prefixing with an underscore.
    $build['_field_layout'] = $display
      ->getLayout()
      ->build($regions);
  }
}