3 namespace Drupal\views\Plugin\views\field;
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;
15 * Provides a field handler which renders an entity in a certain view mode.
17 * @ingroup views_field_handlers
19 * @ViewsField("rendered_entity")
21 class RenderedEntity extends FieldPluginBase implements CacheableDependencyInterface {
23 use EntityTranslationRenderTrait;
28 * @var \Drupal\Core\Entity\EntityManagerInterface
30 protected $entityManager;
33 * The language manager.
35 * @var \Drupal\Core\Language\LanguageManagerInterface
37 protected $languageManager;
40 * Constructs a new RenderedEntity object.
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
50 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
51 * The language manager.
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);
56 $this->entityManager = $entity_manager;
57 $this->languageManager = $language_manager;
63 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
68 $container->get('entity.manager'),
69 $container->get('language_manager')
76 public function usesGroupBy() {
83 public function defineOptions() {
84 $options = parent::defineOptions();
85 $options['view_mode'] = ['default' => 'default'];
93 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
94 parent::buildOptionsForm($form, $form_state);
96 $form['view_mode'] = [
98 '#options' => $this->entityManager->getViewModeOptions($this->getEntityTypeId()),
99 '#title' => $this->t('View mode'),
100 '#default_value' => $this->options['view_mode'],
107 public function render(ResultRow $values) {
108 $entity = $this->getEntityTranslation($this->getEntity($values), $values);
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']);
124 public function getCacheContexts() {
131 public function getCacheTags() {
132 $view_display_storage = $this->entityManager->getStorage('entity_view_display');
133 $view_displays = $view_display_storage->loadMultiple($view_display_storage
135 ->condition('targetEntityType', $this->getEntityTypeId())
139 foreach ($view_displays as $view_display) {
140 $tags = array_merge($tags, $view_display->getCacheTags());
148 public function getCacheMaxAge() {
149 return Cache::PERMANENT;
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);
167 public function getEntityTypeId() {
168 return $this->getEntityType();
174 protected function getEntityManager() {
175 return $this->entityManager;
181 protected function getLanguageManager() {
182 return $this->languageManager;
188 protected function getView() {
195 public function calculateDependencies() {
196 $dependencies = parent::calculateDependencies();
198 $view_mode = $this->entityManager
199 ->getStorage('entity_view_mode')
200 ->load($this->getEntityTypeId() . '.' . $this->options['view_mode']);
202 $dependencies[$view_mode->getConfigDependencyKey()][] = $view_mode->getConfigDependencyName();
205 return $dependencies;