You are here

public function ExtraFieldDisplayManager::entityView in Extra Field 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/ExtraFieldDisplayManager.php \Drupal\extra_field\Plugin\ExtraFieldDisplayManager::entityView()

Appends the renderable data from ExtraField plugins to hook_entity_view().

Parameters

array &$build: A renderable array representing the entity content. The module may add elements to $build prior to rendering. The structure of $build is a renderable array as expected by drupal_render().

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity object.

\Drupal\Core\Entity\Display\EntityViewDisplayInterface $display: The entity view display holding the display options configured for the entity components.

string $viewMode: The view mode the entity is rendered in.

Overrides ExtraFieldDisplayManagerInterface::entityView

File

src/Plugin/ExtraFieldDisplayManager.php, line 102

Class

ExtraFieldDisplayManager
Manages Extra Field display plugins.

Namespace

Drupal\extra_field\Plugin

Code

public function entityView(array &$build, ContentEntityInterface $entity, EntityViewDisplayInterface $display, $viewMode) {
  $definitions = $this
    ->getDefinitions();
  $entityBundleKey = $this
    ->entityBundleKey($entity
    ->getEntityTypeId(), $entity
    ->bundle());
  foreach ($definitions as $pluginId => $definition) {
    if (!$this
      ->matchEntityBundleKey($definition['bundles'], $entityBundleKey)) {
      continue;
    }
    $factory = $this
      ->getFactory();
    if (!$display
      ->getComponent($this
      ->fieldName($pluginId))) {
      continue;
    }

    /** @var ExtraFieldDisplayInterface $plugin */
    $plugin = $factory
      ->createInstance($pluginId);
    $fieldName = $this
      ->fieldName($pluginId);
    $plugin
      ->setEntity($entity);
    $plugin
      ->setEntityViewDisplay($display);
    $plugin
      ->setViewMode($viewMode);
    $elements = $plugin
      ->view($entity);
    if (empty($elements)) {
      continue;
    }
    $build[$fieldName] = $elements;
  }
}