function LayoutBuilderTestEntityHooks::nodeView

Same name and namespace in other branches
  1. 11.x core/modules/layout_builder/tests/modules/layout_builder_test/src/Hook/LayoutBuilderTestEntityHooks.php \Drupal\layout_builder_test\Hook\LayoutBuilderTestEntityHooks::nodeView()

Implements hook_ENTITY_TYPE_view().

Attributes

#[Hook('node_view')]

File

core/modules/layout_builder/tests/modules/layout_builder_test/src/Hook/LayoutBuilderTestEntityHooks.php, line 29

Class

LayoutBuilderTestEntityHooks
Entity hook implementations for layout_builder_test.

Namespace

Drupal\layout_builder_test\Hook

Code

public function nodeView(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) : void {
  if ($display->getComponent('layout_builder_test')) {
    $build['layout_builder_test'] = [
      '#markup' => 'Extra, Extra read all about it.',
    ];
  }
  if ($display->getComponent('layout_builder_test_2')) {
    $build['layout_builder_test_2'] = [
      '#markup' => 'Extra Field 2 is hidden by default.',
    ];
  }
  if ($display->getComponent('layout_builder_test_empty')) {
    $build['layout_builder_test_empty'] = [
      '#cache' => [
        'tags' => [
          'test_extra_fields_empty',
        ],
      ],
    ];
    $render = $this->keyValueFactory
      ->get('test_extra_fields_empty')
      ->get('render_extra_field', FALSE);
    if ($render) {
      $build['layout_builder_test_empty'][] = [
        '#type' => 'markup',
        '#markup' => 'This extra field is visible because it is not empty.',
      ];
    }
  }
}

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