function Entity::render

Same name and namespace in other branches
  1. 9 core/modules/views/src/Plugin/views/area/Entity.php \Drupal\views\Plugin\views\area\Entity::render()
  2. 8.9.x core/modules/views/src/Plugin/views/area/Entity.php \Drupal\views\Plugin\views\area\Entity::render()
  3. 10 core/modules/views/src/Plugin/views/area/Entity.php \Drupal\views\Plugin\views\area\Entity::render()

Overrides AreaPluginBase::render

File

core/modules/views/src/Plugin/views/area/Entity.php, line 178

Class

Entity
Provides an area handler which renders an entity in a certain view mode.

Namespace

Drupal\views\Plugin\views\area

Code

public function render($empty = FALSE) {
    if (!$empty || !empty($this->options['empty'])) {
        // @todo Use a method to check for tokens in
        //   https://www.drupal.org/node/2396607.
        if (str_contains($this->options['target'], '{{')) {
            // We cast as we need the integer/string value provided by the
            // ::tokenizeValue() call.
            $target_id = (string) $this->tokenizeValue($this->options['target']);
            if ($entity = $this->entityTypeManager
                ->getStorage($this->entityType)
                ->load($target_id)) {
                $target_entity = $entity;
            }
        }
        else {
            if ($entity = $this->entityRepository
                ->loadEntityByConfigTarget($this->entityType, $this->options['target'])) {
                $target_entity = $entity;
            }
        }
        if (isset($target_entity) && (!empty($this->options['bypass_access']) || $target_entity->access('view'))) {
            $view_builder = $this->entityTypeManager
                ->getViewBuilder($this->entityType);
            return $view_builder->view($target_entity, $this->options['view_mode']);
        }
    }
    return [];
}

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