3 namespace Drupal\views\Plugin\views\row;
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Language\LanguageManagerInterface;
8 use Drupal\views\Entity\Render\EntityTranslationRenderTrait;
9 use Drupal\views\Plugin\views\display\DisplayPluginBase;
10 use Drupal\views\ViewExecutable;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Generic entity row plugin to provide a common base for all entity types.
18 * deriver = "Drupal\views\Plugin\Derivative\ViewsEntityRow"
21 class EntityRow extends RowPluginBase {
22 use EntityTranslationRenderTrait;
25 * The table the entity is using for storage.
32 * The actual field which is used for the entity id.
39 * Stores the entity type ID of the result entities.
43 protected $entityTypeId;
46 * Contains the entity type of this row plugin instance.
48 * @var \Drupal\Core\Entity\EntityTypeInterface
50 protected $entityType;
55 * @var \Drupal\Core\Entity\EntityManagerInterface
57 public $entityManager;
60 * The language manager.
62 * @var \Drupal\Core\Language\LanguageManagerInterface
64 protected $languageManager;
69 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
71 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
72 * The language manager.
74 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager) {
75 parent::__construct($configuration, $plugin_id, $plugin_definition);
77 $this->entityManager = $entity_manager;
78 $this->languageManager = $language_manager;
84 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
85 parent::init($view, $display, $options);
87 $this->entityTypeId = $this->definition['entity_type'];
88 $this->entityType = $this->entityManager->getDefinition($this->entityTypeId);
89 $this->base_table = $this->entityType->getDataTable() ?: $this->entityType->getBaseTable();
90 $this->base_field = $this->entityType->getKey('id');
96 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
97 return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity.manager'), $container->get('language_manager'));
103 public function getEntityTypeId() {
104 return $this->entityType->id();
110 protected function getEntityManager() {
111 return $this->entityManager;
117 protected function getLanguageManager() {
118 return $this->languageManager;
124 protected function getView() {
131 protected function defineOptions() {
132 $options = parent::defineOptions();
133 $options['view_mode'] = ['default' => 'default'];
140 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
141 parent::buildOptionsForm($form, $form_state);
143 $form['view_mode'] = [
145 '#options' => \Drupal::entityManager()->getViewModeOptions($this->entityTypeId),
146 '#title' => $this->t('View mode'),
147 '#default_value' => $this->options['view_mode'],
154 public function summaryTitle() {
155 $options = \Drupal::entityManager()->getViewModeOptions($this->entityTypeId);
156 if (isset($options[$this->options['view_mode']])) {
157 return $options[$this->options['view_mode']];
160 return $this->t('No view mode selected');
167 public function query() {
169 $this->getEntityTranslationRenderer()->query($this->view->getQuery());
175 public function preRender($result) {
176 parent::preRender($result);
178 $this->getEntityTranslationRenderer()->preRender($result);
185 public function render($row) {
186 return $this->getEntityTranslationRenderer()->render($row);
192 public function calculateDependencies() {
193 $dependencies = parent::calculateDependencies();
195 $view_mode = $this->entityManager
196 ->getStorage('entity_view_mode')
197 ->load($this->entityTypeId . '.' . $this->options['view_mode']);
199 $dependencies[$view_mode->getConfigDependencyKey()][] = $view_mode->getConfigDependencyName();
202 return $dependencies;