View source
<?php
namespace Drupal\entity\Plugin\views\field;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\views\Entity\Render\EntityTranslationRenderTrait;
use Drupal\views\ResultRow;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RenderedEntity extends FieldPluginBase implements CacheableDependencyInterface {
use EntityTranslationRenderTrait;
protected $entityManager;
protected $languageManager;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityManager = $entity_manager;
$this->languageManager = $language_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity.manager'), $container
->get('language_manager'));
}
public function usesGroupBy() {
return FALSE;
}
public function defineOptions() {
$options = parent::defineOptions();
$options['view_mode'] = [
'default' => 'default',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['view_mode'] = [
'#type' => 'select',
'#options' => $this->entityManager
->getViewModeOptions($this
->getEntityTypeId()),
'#title' => $this
->t('View mode'),
'#default_value' => $this->options['view_mode'],
];
}
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->entityManager
->getViewBuilder($this
->getEntityTypeId());
$build += $view_builder
->view($entity, $this->options['view_mode']);
}
}
return $build;
}
public function getCacheContexts() {
return [];
}
public function getCacheTags() {
$view_display_storage = $this->entityManager
->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 = array_merge($tags, $view_display
->getCacheTags());
}
return $tags;
}
public function getCacheMaxAge() {
return 0;
}
public function query() {
if ($this->languageManager
->isMultilingual()) {
$this
->getEntityTranslationRenderer()
->query($this->query, $this->relationship);
}
}
public function getEntityTypeId() {
return $this
->getEntityType();
}
protected function getEntityManager() {
return $this->entityManager;
}
protected function getLanguageManager() {
return $this->languageManager;
}
protected function getView() {
return $this->view;
}
public function calculateDependencies() {
$dependencies = parent::calculateDependencies();
$view_mode = $this->entityManager
->getStorage('entity_view_mode')
->load($this
->getEntityTypeId() . '.' . $this->options['view_mode']);
if ($view_mode) {
$dependencies[$view_mode
->getConfigDependencyKey()][] = $view_mode
->getConfigDependencyName();
}
return $dependencies;
}
}