You are here

class RenderedEntity in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/field/RenderedEntity.php \Drupal\views\Plugin\views\field\RenderedEntity
  2. 9 core/modules/views/src/Plugin/views/field/RenderedEntity.php \Drupal\views\Plugin\views\field\RenderedEntity

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

Plugin annotation

@ViewsField("rendered_entity");

Hierarchy

Expanded class hierarchy of RenderedEntity

File

core/modules/views/src/Plugin/views/field/RenderedEntity.php, line 23

Namespace

Drupal\views\Plugin\views\field
View source
class RenderedEntity extends FieldPluginBase implements CacheableDependencyInterface {
  use EntityTranslationRenderTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity repository service.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * The entity display repository.
   *
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
   */
  protected $entityDisplayRepository;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Constructs a new RenderedEntity object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param array $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository.
   * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
   *   The entity display repository.
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, EntityRepositoryInterface $entity_repository, EntityDisplayRepositoryInterface $entity_display_repository) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
    $this->languageManager = $language_manager;
    $this->entityRepository = $entity_repository;
    $this->entityDisplayRepository = $entity_display_repository;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('language_manager'), $container
      ->get('entity.repository'), $container
      ->get('entity_display.repository'));
  }

  /**
   * {@inheritdoc}
   */
  public function usesGroupBy() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function defineOptions() {
    $options = parent::defineOptions();
    $options['view_mode'] = [
      'default' => 'default',
    ];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    $form['view_mode'] = [
      '#type' => 'select',
      '#options' => $this->entityDisplayRepository
        ->getViewModeOptions($this
        ->getEntityTypeId()),
      '#title' => $this
        ->t('View mode'),
      '#default_value' => $this->options['view_mode'],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $entity = $this
      ->getEntityTranslation($this
      ->getEntity($values), $values);
    $build = [];
    if (isset($entity)) {
      $access = $entity
        ->access('view', NULL, TRUE);
      $build['#access'] = $access;
      if ($access
        ->isAllowed()) {
        $view_builder = $this->entityTypeManager
          ->getViewBuilder($this
          ->getEntityTypeId());
        $build += $view_builder
          ->view($entity, $this->options['view_mode'], $entity
          ->language()
          ->getId());
      }
    }
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    $view_display_storage = $this->entityTypeManager
      ->getStorage('entity_view_display');
    $view_displays = $view_display_storage
      ->loadMultiple($view_display_storage
      ->getQuery()
      ->condition('targetEntityType', $this
      ->getEntityTypeId())
      ->execute());
    $tags = [];
    foreach ($view_displays as $view_display) {
      $tags[] = $view_display
        ->getCacheTags();
    }
    return array_merge([], ...$tags);
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return Cache::PERMANENT;
  }

  /**
   * {@inheritdoc}
   */
  public function query() {

    // We purposefully do not call parent::query() because we do not want the
    // default query behavior for Views fields. Instead, let the entity
    // translation renderer provide the correct query behavior.
    if ($this->languageManager
      ->isMultilingual()) {
      $this
        ->getEntityTranslationRenderer()
        ->query($this->query, $this->relationship);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityTypeId() {
    return $this
      ->getEntityType();
  }

  /**
   * {@inheritdoc}
   */
  protected function getEntityTypeManager() {
    return $this->entityTypeManager;
  }

  /**
   * {@inheritdoc}
   */
  protected function getEntityRepository() {
    return $this->entityRepository;
  }

  /**
   * {@inheritdoc}
   */
  protected function getLanguageManager() {
    return $this->languageManager;
  }

  /**
   * {@inheritdoc}
   */
  protected function getView() {
    return $this->view;
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $dependencies = parent::calculateDependencies();
    $view_mode = $this->entityTypeManager
      ->getStorage('entity_view_mode')
      ->load($this
      ->getEntityTypeId() . '.' . $this->options['view_mode']);
    if ($view_mode) {
      $dependencies[$view_mode
        ->getConfigDependencyKey()][] = $view_mode
        ->getConfigDependencyName();
    }
    return $dependencies;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityTranslationRenderTrait::$entityTranslationRenderer protected property The renderer to be used to render the entity row.
EntityTranslationRenderTrait::getEntityTranslation public function Returns the entity translation matching the configured row language.
EntityTranslationRenderTrait::getEntityTranslationRenderer protected function Returns the current renderer.
RenderedEntity::$entityDisplayRepository protected property The entity display repository.
RenderedEntity::$entityRepository protected property The entity repository service.
RenderedEntity::$entityTypeManager protected property The entity type manager.
RenderedEntity::$languageManager protected property The language manager.
RenderedEntity::buildOptionsForm public function
RenderedEntity::calculateDependencies public function
RenderedEntity::create public static function
RenderedEntity::defineOptions public function
RenderedEntity::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts
RenderedEntity::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge
RenderedEntity::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags
RenderedEntity::getEntityRepository protected function
RenderedEntity::getEntityTypeId public function Returns the entity type identifier. Overrides EntityTranslationRenderTrait::getEntityTypeId
RenderedEntity::getEntityTypeManager protected function
RenderedEntity::getLanguageManager protected function Returns the language manager. Overrides EntityTranslationRenderTrait::getLanguageManager
RenderedEntity::getView protected function Returns the top object of a view. Overrides EntityTranslationRenderTrait::getView
RenderedEntity::query public function
RenderedEntity::render public function
RenderedEntity::usesGroupBy public function
RenderedEntity::__construct public function Constructs a new RenderedEntity object.