function layout_builder_entity_view_alter

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/layout_builder.module \layout_builder_entity_view_alter()
  2. 8.9.x core/modules/layout_builder/layout_builder.module \layout_builder_entity_view_alter()
  3. 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()

1 call to layout_builder_entity_view_alter()
EntityViewAlterTest::testContextualLinksRemoved in core/modules/layout_builder/tests/src/Kernel/EntityViewAlterTest.php
Tests that contextual links are removed when rendering Layout Builder.

File

core/modules/layout_builder/layout_builder.module, line 135

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 = $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 ($route_name && $display instanceof LayoutBuilderEntityViewDisplay && str_starts_with($route_name, 'layout_builder.')) {
        unset($build['#contextual_links']);
    }
}

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