3 namespace Drupal\views\Plugin\views\area;
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Plugin\views\display\DisplayPluginBase;
8 use Drupal\views\ViewExecutable;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Provides an area handler which renders an entity in a certain view mode.
14 * @ingroup views_area_handlers
16 * @ViewsArea("entity")
18 class Entity extends TokenizeAreaPluginBase {
21 * Stores the entity type of the result entities.
25 protected $entityType;
30 * @var \Drupal\Core\Entity\EntityManagerInterface
32 protected $entityManager;
35 * Constructs a new Entity instance.
37 * @param array $configuration
38 * A configuration array containing information about the plugin instance.
39 * @param string $plugin_id
40 * The plugin_id for the plugin instance.
41 * @param mixed $plugin_definition
42 * The plugin implementation definition.
43 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
46 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
47 parent::__construct($configuration, $plugin_id, $plugin_definition);
49 $this->entityManager = $entity_manager;
55 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
60 $container->get('entity.manager')
67 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
68 parent::init($view, $display, $options);
69 $this->entityType = $this->definition['entity_type'];
75 protected function defineOptions() {
76 $options = parent::defineOptions();
78 // Per default we enable tokenize, as this is the most common use case for
80 $options['tokenize']['default'] = TRUE;
82 // Contains the config target identifier for the entity.
83 $options['target'] = ['default' => ''];
84 $options['view_mode'] = ['default' => 'default'];
85 $options['bypass_access'] = ['default' => FALSE];
93 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
94 parent::buildOptionsForm($form, $form_state);
96 $form['view_mode'] = [
98 '#options' => $this->entityManager->getViewModeOptions($this->entityType),
99 '#title' => $this->t('View mode'),
100 '#default_value' => $this->options['view_mode'],
103 $label = $this->entityManager->getDefinition($this->entityType)->getLabel();
104 $target = $this->options['target'];
106 // If the target does not contain tokens, try to load the entity and
107 // display the entity ID to the admin form user.
108 // @todo Use a method to check for tokens in
109 // https://www.drupal.org/node/2396607.
110 if (strpos($this->options['target'], '{{') === FALSE) {
111 // @todo If the entity does not exist, this will will show the config
112 // target identifier. Decide if this is the correct behavior in
113 // https://www.drupal.org/node/2415391.
114 if ($target_entity = $this->entityManager->loadEntityByConfigTarget($this->entityType, $this->options['target'])) {
115 $target = $target_entity->id();
119 '#title' => $this->t('@entity_type_label ID', ['@entity_type_label' => $label]),
120 '#type' => 'textfield',
121 '#default_value' => $target,
124 $form['bypass_access'] = [
125 '#type' => 'checkbox',
126 '#title' => $this->t('Bypass access checks'),
127 '#description' => $this->t('If enabled, access permissions for rendering the entity are not checked.'),
128 '#default_value' => !empty($this->options['bypass_access']),
135 public function submitOptionsForm(&$form, FormStateInterface $form_state) {
136 parent::submitOptionsForm($form, $form_state);
138 // Load the referenced entity and store its config target identifier if
139 // the target does not contains tokens.
140 // @todo Use a method to check for tokens in
141 // https://www.drupal.org/node/2396607.
142 $options = $form_state->getValue('options');
143 if (strpos($options['target'], '{{') === FALSE) {
144 if ($entity = $this->entityManager->getStorage($this->entityType)->load($options['target'])) {
145 $options['target'] = $entity->getConfigTarget();
147 $form_state->setValue('options', $options);
154 public function render($empty = FALSE) {
155 if (!$empty || !empty($this->options['empty'])) {
156 // @todo Use a method to check for tokens in
157 // https://www.drupal.org/node/2396607.
158 if (strpos($this->options['target'], '{{') !== FALSE) {
159 // We cast as we need the integer/string value provided by the
160 // ::tokenizeValue() call.
161 $target_id = (string) $this->tokenizeValue($this->options['target']);
162 if ($entity = $this->entityManager->getStorage($this->entityType)->load($target_id)) {
163 $target_entity = $entity;
167 if ($entity = $this->entityManager->loadEntityByConfigTarget($this->entityType, $this->options['target'])) {
168 $target_entity = $entity;
171 if (isset($target_entity) && (!empty($this->options['bypass_access']) || $target_entity->access('view'))) {
172 $view_builder = $this->entityManager->getViewBuilder($this->entityType);
173 return $view_builder->view($target_entity, $this->options['view_mode']);
183 public function calculateDependencies() {
184 $dependencies = parent::calculateDependencies();
186 // Ensure that we don't add dependencies for placeholders.
187 // @todo Use a method to check for tokens in
188 // https://www.drupal.org/node/2396607.
189 if (strpos($this->options['target'], '{{') === FALSE) {
190 if ($entity = $this->entityManager->loadEntityByConfigTarget($this->entityType, $this->options['target'])) {
191 $dependencies[$this->entityManager->getDefinition($this->entityType)->getConfigDependencyKey()][] = $entity->getConfigDependencyName();
195 return $dependencies;