Version 1
[yaffs-website] / web / core / modules / views / src / Plugin / views / field / RenderedEntity.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\field;
4
5 use Drupal\Core\Cache\CacheableDependencyInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Language\LanguageManagerInterface;
9 use Drupal\views\Entity\Render\EntityTranslationRenderTrait;
10 use Drupal\views\ResultRow;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Drupal\Core\Cache\Cache;
13
14 /**
15  * Provides a field handler which renders an entity in a certain view mode.
16  *
17  * @ingroup views_field_handlers
18  *
19  * @ViewsField("rendered_entity")
20  */
21 class RenderedEntity extends FieldPluginBase implements CacheableDependencyInterface {
22
23   use EntityTranslationRenderTrait;
24
25   /**
26    * The entity manager.
27    *
28    * @var \Drupal\Core\Entity\EntityManagerInterface
29    */
30   protected $entityManager;
31
32   /**
33    * The language manager.
34    *
35    * @var \Drupal\Core\Language\LanguageManagerInterface
36    */
37   protected $languageManager;
38
39   /**
40    * Constructs a new RenderedEntity object.
41    *
42    * @param array $configuration
43    *   A configuration array containing information about the plugin instance.
44    * @param string $plugin_id
45    *   The plugin_id for the plugin instance.
46    * @param array $plugin_definition
47    *   The plugin implementation definition.
48    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
49    *    The entity manager.
50    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
51    *   The language manager.
52    */
53   public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager) {
54     parent::__construct($configuration, $plugin_id, $plugin_definition);
55
56     $this->entityManager = $entity_manager;
57     $this->languageManager = $language_manager;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
64     return new static(
65       $configuration,
66       $plugin_id,
67       $plugin_definition,
68       $container->get('entity.manager'),
69       $container->get('language_manager')
70     );
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function usesGroupBy() {
77     return FALSE;
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function defineOptions() {
84     $options = parent::defineOptions();
85     $options['view_mode'] = ['default' => 'default'];
86
87     return $options;
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
94     parent::buildOptionsForm($form, $form_state);
95
96     $form['view_mode'] = [
97       '#type' => 'select',
98       '#options' => $this->entityManager->getViewModeOptions($this->getEntityTypeId()),
99       '#title' => $this->t('View mode'),
100       '#default_value' => $this->options['view_mode'],
101     ];
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function render(ResultRow $values) {
108     $entity = $this->getEntityTranslation($this->getEntity($values), $values);
109     $build = [];
110     if (isset($entity)) {
111       $access = $entity->access('view', NULL, TRUE);
112       $build['#access'] = $access;
113       if ($access->isAllowed()) {
114         $view_builder = $this->entityManager->getViewBuilder($this->getEntityTypeId());
115         $build += $view_builder->view($entity, $this->options['view_mode']);
116       }
117     }
118     return $build;
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function getCacheContexts() {
125     return [];
126   }
127
128   /**
129    * {@inheritdoc}
130    */
131   public function getCacheTags() {
132     $view_display_storage = $this->entityManager->getStorage('entity_view_display');
133     $view_displays = $view_display_storage->loadMultiple($view_display_storage
134       ->getQuery()
135       ->condition('targetEntityType', $this->getEntityTypeId())
136       ->execute());
137
138     $tags = [];
139     foreach ($view_displays as $view_display) {
140       $tags = array_merge($tags, $view_display->getCacheTags());
141     }
142     return $tags;
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public function getCacheMaxAge() {
149     return Cache::PERMANENT;
150   }
151
152   /**
153    * {@inheritdoc}
154    */
155   public function query() {
156     // We purposefully do not call parent::query() because we do not want the
157     // default query behavior for Views fields. Instead, let the entity
158     // translation renderer provide the correct query behavior.
159     if ($this->languageManager->isMultilingual()) {
160       $this->getEntityTranslationRenderer()->query($this->query, $this->relationship);
161     }
162   }
163
164   /**
165    * {@inheritdoc}
166    */
167   public function getEntityTypeId() {
168     return $this->getEntityType();
169   }
170
171   /**
172    * {@inheritdoc}
173    */
174   protected function getEntityManager() {
175     return $this->entityManager;
176   }
177
178   /**
179    * {@inheritdoc}
180    */
181   protected function getLanguageManager() {
182     return $this->languageManager;
183   }
184
185   /**
186    * {@inheritdoc}
187    */
188   protected function getView() {
189     return $this->view;
190   }
191
192   /**
193    * {@inheritdoc}
194    */
195   public function calculateDependencies() {
196     $dependencies = parent::calculateDependencies();
197
198     $view_mode = $this->entityManager
199       ->getStorage('entity_view_mode')
200       ->load($this->getEntityTypeId() . '.' . $this->options['view_mode']);
201     if ($view_mode) {
202       $dependencies[$view_mode->getConfigDependencyKey()][] = $view_mode->getConfigDependencyName();
203     }
204
205     return $dependencies;
206   }
207
208 }