Version 1
[yaffs-website] / web / core / modules / views / src / Plugin / views / area / Entity.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\area;
4
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;
10
11 /**
12  * Provides an area handler which renders an entity in a certain view mode.
13  *
14  * @ingroup views_area_handlers
15  *
16  * @ViewsArea("entity")
17  */
18 class Entity extends TokenizeAreaPluginBase {
19
20   /**
21    * Stores the entity type of the result entities.
22    *
23    * @var string
24    */
25   protected $entityType;
26
27   /**
28    * The entity manager.
29    *
30    * @var \Drupal\Core\Entity\EntityManagerInterface
31    */
32   protected $entityManager;
33
34   /**
35    * Constructs a new Entity instance.
36    *
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
44    *   The entity manager.
45    */
46   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
47     parent::__construct($configuration, $plugin_id, $plugin_definition);
48
49     $this->entityManager = $entity_manager;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
56     return new static(
57       $configuration,
58       $plugin_id,
59       $plugin_definition,
60       $container->get('entity.manager')
61     );
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
68     parent::init($view, $display, $options);
69     $this->entityType = $this->definition['entity_type'];
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   protected function defineOptions() {
76     $options = parent::defineOptions();
77
78     // Per default we enable tokenize, as this is the most common use case for
79     // this handler.
80     $options['tokenize']['default'] = TRUE;
81
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];
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->entityType),
99       '#title' => $this->t('View mode'),
100       '#default_value' => $this->options['view_mode'],
101     ];
102
103     $label = $this->entityManager->getDefinition($this->entityType)->getLabel();
104     $target = $this->options['target'];
105
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();
116       }
117     }
118     $form['target'] = [
119       '#title' => $this->t('@entity_type_label ID', ['@entity_type_label' => $label]),
120       '#type' => 'textfield',
121       '#default_value' => $target,
122     ];
123
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']),
129     ];
130   }
131
132   /**
133    * {@inheritdoc}
134    */
135   public function submitOptionsForm(&$form, FormStateInterface $form_state) {
136     parent::submitOptionsForm($form, $form_state);
137
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();
146       }
147       $form_state->setValue('options', $options);
148     }
149   }
150
151   /**
152    * {@inheritdoc}
153    */
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;
164         }
165       }
166       else {
167         if ($entity = $this->entityManager->loadEntityByConfigTarget($this->entityType, $this->options['target'])) {
168           $target_entity = $entity;
169         }
170       }
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']);
174       }
175     }
176
177     return [];
178   }
179
180   /**
181    * {@inheritdoc}
182    */
183   public function calculateDependencies() {
184     $dependencies = parent::calculateDependencies();
185
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();
192       }
193     }
194
195     return $dependencies;
196   }
197
198 }