You are here

protected function FieldLayoutBuilder::getFields in Drupal 9

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

Gets the fields that need to be processed.

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.

string $display_context: The display context, either 'form' or 'view'.

Return value

array An array of configurable fields present in the build.

2 calls to FieldLayoutBuilder::getFields()
FieldLayoutBuilder::buildForm in core/modules/field_layout/src/FieldLayoutBuilder.php
Applies the layout to an entity form.
FieldLayoutBuilder::buildView in core/modules/field_layout/src/FieldLayoutBuilder.php
Applies the layout to an entity build.

File

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

Class

FieldLayoutBuilder
Builds a field layout.

Namespace

Drupal\field_layout

Code

protected function getFields(array $build, EntityDisplayWithLayoutInterface $display, $display_context) {
  $components = $display
    ->getComponents();

  // Ignore any extra fields from the list of field definitions. Field
  // definitions can have a non-configurable display, but all extra fields are
  // always displayed.
  $field_definitions = array_diff_key($this->entityFieldManager
    ->getFieldDefinitions($display
    ->getTargetEntityTypeId(), $display
    ->getTargetBundle()), $this->entityFieldManager
    ->getExtraFields($display
    ->getTargetEntityTypeId(), $display
    ->getTargetBundle()));
  $fields_to_exclude = array_filter($field_definitions, function (FieldDefinitionInterface $field_definition) use ($display_context) {

    // Remove fields with a non-configurable display.
    return !$field_definition
      ->isDisplayConfigurable($display_context);
  });
  $components = array_diff_key($components, $fields_to_exclude);

  // Only include fields present in the build.
  $components = array_intersect_key($components, $build);
  return $components;
}