You are here

class EntityRow in Drupal 10

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

Generic entity row plugin to provide a common base for all entity types.

Plugin annotation


@ViewsRow(
  id = "entity",
  deriver = "Drupal\views\Plugin\Derivative\ViewsEntityRow"
)

Hierarchy

Expanded class hierarchy of EntityRow

2 files declare their use of EntityRow
NodeRow.php in core/modules/node/src/Plugin/views/row/NodeRow.php
UserRow.php in core/modules/user/src/Plugin/views/row/UserRow.php

File

core/modules/views/src/Plugin/views/row/EntityRow.php, line 23

Namespace

Drupal\views\Plugin\views\row
View source
class EntityRow extends RowPluginBase {
  use EntityTranslationRenderTrait;

  /**
   * The table the entity is using for storage.
   *
   * @var string
   */
  public $base_table;

  /**
   * Stores the entity type ID of the result entities.
   *
   * @var string
   */
  protected $entityTypeId;

  /**
   * Contains the entity type of this row plugin instance.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface
   */
  protected $entityType;

  /**
   * 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 EntityRow 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 = NULL, EntityDisplayRepositoryInterface $entity_display_repository = NULL) {
    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 function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
    $this->entityTypeId = $this->definition['entity_type'];
    $this->entityType = $this->entityTypeManager
      ->getDefinition($this->entityTypeId);
    $this->base_table = $this->entityType
      ->getDataTable() ?: $this->entityType
      ->getBaseTable();
    $this->base_field = $this->entityType
      ->getKey('id');
  }

  /**
   * {@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 getEntityTypeId() {
    return $this->entityType
      ->id();
  }

  /**
   * {@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}
   */
  protected 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->entityTypeId),
      '#title' => $this
        ->t('View mode'),
      '#default_value' => $this->options['view_mode'],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function summaryTitle() {
    $options = $this->entityDisplayRepository
      ->getViewModeOptions($this->entityTypeId);
    if (isset($options[$this->options['view_mode']])) {
      return $options[$this->options['view_mode']];
    }
    else {
      return $this
        ->t('No view mode selected');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
    parent::query();
    $this
      ->getEntityTranslationRenderer()
      ->query($this->view
      ->getQuery());
  }

  /**
   * {@inheritdoc}
   */
  public function preRender($result) {
    parent::preRender($result);
    if ($result) {
      $this
        ->getEntityTranslationRenderer()
        ->preRender($result);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function render($row) {
    return $this
      ->getEntityTranslationRenderer()
      ->render($row);
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
EntityRow::$base_table public property The table the entity is using for storage.
EntityRow::$entityDisplayRepository protected property The entity display repository.
EntityRow::$entityRepository protected property The entity repository service.
EntityRow::$entityType protected property Contains the entity type of this row plugin instance.
EntityRow::$entityTypeId protected property Stores the entity type ID of the result entities.
EntityRow::$entityTypeManager protected property The entity type manager.
EntityRow::$languageManager protected property The language manager.
EntityRow::buildOptionsForm public function
EntityRow::calculateDependencies public function
EntityRow::create public static function
EntityRow::defineOptions protected function 2
EntityRow::getEntityRepository protected function
EntityRow::getEntityTypeId public function Returns the entity type identifier. Overrides EntityTranslationRenderTrait::getEntityTypeId
EntityRow::getEntityTypeManager protected function
EntityRow::getLanguageManager protected function Returns the language manager. Overrides EntityTranslationRenderTrait::getLanguageManager
EntityRow::getView protected function Returns the top object of a view. Overrides EntityTranslationRenderTrait::getView
EntityRow::init public function
EntityRow::preRender public function
EntityRow::query public function
EntityRow::render public function
EntityRow::summaryTitle public function
EntityRow::__construct public function Constructs a new EntityRow object.
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.