You are here

function layout_builder_entity_view_alter in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/layout_builder/layout_builder.module \layout_builder_entity_view_alter()
  2. 10 core/modules/layout_builder/layout_builder.module \layout_builder_entity_view_alter()

Implements hook_entity_view_alter().

ExtraFieldBlock block plugins add placeholders for each extra field which is configured to be displayed. Those placeholders are replaced by this hook. Modules that implement hook_entity_extra_field_info() use their implementations of hook_entity_view_alter() to add the rendered output of the extra fields they provide, so we cannot get the rendered output of extra fields before this point in the view process. layout_builder_module_implements_alter() moves this implementation of hook_entity_view_alter() to the end of the list.

See also

\Drupal\layout_builder\Plugin\Block\ExtraFieldBlock::build()

layout_builder_module_implements_alter()

File

core/modules/layout_builder/layout_builder.module, line 136
Provides hook implementations for Layout Builder.

Code

function layout_builder_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {

  // Only replace extra fields when Layout Builder has been used to alter the
  // build. See \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::buildMultiple().
  if (isset($build['_layout_builder']) && !Element::isEmpty($build['_layout_builder'])) {

    /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
    $field_manager = \Drupal::service('entity_field.manager');
    $extra_fields = $field_manager
      ->getExtraFields($entity
      ->getEntityTypeId(), $entity
      ->bundle());
    if (!empty($extra_fields['display'])) {
      foreach ($extra_fields['display'] as $field_name => $extra_field) {

        // If the extra field is not set replace with an empty array to avoid
        // the placeholder text from being rendered.
        $replacement = isset($build[$field_name]) ? $build[$field_name] : [];
        ExtraFieldBlock::replaceFieldPlaceholder($build, $replacement, $field_name);

        // After the rendered field in $build has been copied over to the
        // ExtraFieldBlock block we must remove it from its original location or
        // else it will be rendered twice.
        unset($build[$field_name]);
      }
    }
  }
  $route_name = \Drupal::routeMatch()
    ->getRouteName();

  // If the entity is displayed within a Layout Builder block and the current
  // route is in the Layout Builder UI, then remove all contextual link
  // placeholders.
  if ($display instanceof LayoutBuilderEntityViewDisplay && strpos($route_name, 'layout_builder.') === 0) {
    unset($build['#contextual_links']);
  }
  if (\Drupal::moduleHandler()
    ->moduleExists('quickedit')) {

    /** @var \Drupal\layout_builder\QuickEditIntegration $quick_edit_integration */
    $quick_edit_integration = \Drupal::classResolver(QuickEditIntegration::class);
    $quick_edit_integration
      ->entityViewAlter($build, $entity, $display);
  }
}