Interim commit.
[yaffs-website] / web / modules / contrib / entity_embed / src / EntityEmbedDisplay / EntityEmbedDisplayBase.php
1 <?php
2
3 namespace Drupal\entity_embed\EntityEmbedDisplay;
4
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Core\Access\AccessResult;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Language\LanguageInterface;
10 use Drupal\Core\Language\LanguageManagerInterface;
11 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
12 use Drupal\Core\Plugin\PluginBase;
13 use Drupal\Core\Session\AccountInterface;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15
16 /**
17  * Defines a base Entity Embed Display implementation.
18  *
19  * @see \Drupal\entity_embed\Annotation\EntityEmbedDisplay
20  * @see \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayInterface
21  * @see \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager
22  * @see plugin_api
23  *
24  * @ingroup entity_embed_api
25  */
26 abstract class EntityEmbedDisplayBase extends PluginBase implements ContainerFactoryPluginInterface, EntityEmbedDisplayInterface {
27
28   /**
29    * The entity type manager service.
30    *
31    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
32    */
33   protected $entityTypeManager;
34
35   /**
36    * The language manager.
37    *
38    * @var \Drupal\Core\Language\LanguageManagerInterface $language_manager
39    */
40   protected $languageManager;
41
42   /**
43    * The context for the plugin.
44    *
45    * @var array
46    */
47   public $context = array();
48
49   /**
50    * The attributes on the embedded entity.
51    *
52    * @var array
53    */
54   public $attributes = array();
55
56   /**
57    * {@inheritdoc}
58    *
59    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
60    *   The entity type manager service.
61    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
62    *   The language manager.
63    */
64   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) {
65     parent::__construct($configuration, $plugin_id, $plugin_definition);
66     $this->setConfiguration($configuration);
67     $this->entityTypeManager = $entity_type_manager;
68     $this->languageManager = $language_manager;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
75     return new static(
76       $configuration,
77       $plugin_id,
78       $plugin_definition,
79       $container->get('entity_type.manager'),
80       $container->get('language_manager')
81     );
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function access(AccountInterface $account = NULL) {
88     // @todo Add a hook_entity_embed_display_access()?
89     // Check that the plugin's registered entity types matches the current
90     // entity type.
91     return AccessResult::allowedIf($this->isValidEntityType())
92       // @see \Drupal\Core\Entity\EntityTypeManager
93       ->addCacheTags(['entity_types']);
94   }
95
96   /**
97    * Validates that this Entity Embed Display plugin applies to the current
98    * entity type.
99    *
100    * This checks the plugin annotation's 'entity_types' value, which should be
101    * an array of entity types that this plugin can process, or FALSE if the
102    * plugin applies to all entity types.
103    *
104    * @return bool
105    *   TRUE if the plugin can display the current entity type, or FALSE
106    *   otherwise.
107    */
108   protected function isValidEntityType() {
109     // First, determine whether or not the entity type id is valid. Return FALSE
110     // if the specified id is not valid.
111     $entity_type = $this->getEntityTypeFromContext();
112     if (!$this->entityTypeManager->getDefinition($entity_type)) {
113       return FALSE;
114     }
115
116     $definition = $this->getPluginDefinition();
117     if ($definition['entity_types'] === FALSE) {
118       return TRUE;
119     }
120     else {
121       return in_array($entity_type, $definition['entity_types']);
122     }
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   abstract public function build();
129
130   /**
131    * {@inheritdoc}
132    */
133   public function calculateDependencies() {
134     return array();
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function defaultConfiguration() {
141     return array();
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   public function getConfiguration() {
148     return $this->configuration;
149   }
150
151   /**
152    * {@inheritdoc}
153    */
154   public function setConfiguration(array $configuration) {
155     $this->configuration = NestedArray::mergeDeep(
156       $this->defaultConfiguration(),
157       $configuration
158     );
159     return $this;
160   }
161
162   /**
163    * {@inheritdoc}
164    */
165   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
166     return $form;
167   }
168
169   /**
170    * {@inheritdoc}
171    */
172   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
173     // Do nothing.
174   }
175
176   /**
177    * {@inheritdoc}
178    */
179   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
180     if (!$form_state->getErrors()) {
181       $this->configuration = array_intersect_key($form_state->getValues(), $this->defaultConfiguration());
182     }
183   }
184
185   /**
186    * Gets a configuration value.
187    *
188    * @param string $name
189    *   The name of the plugin configuration value.
190    * @param mixed $default
191    *   The default value to return if the configuration value does not exist.
192    *
193    * @return mixed
194    *   The currently set configuration value, or the value of $default if the
195    *   configuration value is not set.
196    */
197   public function getConfigurationValue($name, $default = NULL) {
198     $configuration = $this->getConfiguration();
199     return array_key_exists($name, $configuration) ? $configuration[$name] : $default;
200   }
201
202   /**
203    * Sets the value for a defined context.
204    *
205    * @param string $name
206    *   The name of the context in the plugin definition.
207    * @param mixed $value
208    *   The value to set the context to. The value has to validate against the
209    *   provided context definition.
210    */
211   public function setContextValue($name, $value) {
212     $this->context[$name] = $value;
213   }
214
215   /**
216    * Gets the values for all defined contexts.
217    *
218    * @return array
219    *   An array of set context values, keyed by context name.
220    */
221   public function getContextValues() {
222     return $this->context;
223   }
224
225   /**
226    * Gets the value for a defined context.
227    *
228    * @param string $name
229    *   The name of the context in the plugin configuration.
230    *
231    * @return mixed
232    *   The currently set context value.
233    */
234   public function getContextValue($name) {
235     return $this->context[$name];
236   }
237
238   /**
239    * Returns whether or not value is set for a defined context.
240    *
241    * @param string $name
242    *   The name of the context in the plugin configuration.
243    *
244    * @return bool
245    *   True if context value exists, false otherwise.
246    */
247   public function hasContextValue($name) {
248     return array_key_exists($name, $this->context);
249   }
250
251   /**
252    * Gets the entity type from the current context.
253    *
254    * @return string
255    *   The entity type id.
256    */
257   public function getEntityTypeFromContext() {
258     if ($this->hasContextValue('entity')) {
259       return $this->getContextValue('entity')->getEntityTypeId();
260     }
261     else {
262       return $this->getContextValue('entity_type');
263     }
264   }
265
266   /**
267    * Gets the entity from the current context.
268    *
269    * @todo Where doe sthis come from? The value must come from somewhere, yet
270    * this does not implement any context-related interfaces. This is an *input*,
271    * so we need cache contexts and possibly cache tags to reflect where this
272    * came from. We need that for *everything* that this class does that relies
273    * on this, plus any of its subclasses. Right now, this is effectively a
274    * global that breaks cacheability metadata.
275    *
276    * @return \Drupal\Core\Entity\EntityInterface
277    */
278   public function getEntityFromContext() {
279     if ($this->hasContextValue('entity')) {
280       return $this->getContextValue('entity');
281     }
282   }
283
284   /**
285    * Sets the values for all attributes.
286    *
287    * @param array $attributes
288    *   An array of attributes, keyed by attribute name.
289    */
290   public function setAttributes(array $attributes) {
291     $this->attributes = $attributes;
292   }
293
294   /**
295    * Gets the values for all attributes.
296    *
297    * @return array
298    *   An array of set attribute values, keyed by attribute name.
299    */
300   public function getAttributeValues() {
301     return $this->attributes;
302   }
303
304   /**
305    * Gets the value for an attribute.
306    *
307    * @param string $name
308    *   The name of the attribute.
309    * @param mixed $default
310    *   The default value to return if the attribute value does not exist.
311    *
312    * @return mixed
313    *   The currently set attribute value.
314    */
315   public function getAttributeValue($name, $default = NULL) {
316     $attributes = $this->getAttributeValues();
317     return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
318   }
319
320   /**
321    * Gets the current language code.
322    *
323    * @return string
324    */
325   public function getLangcode() {
326     $langcode = $this->getAttributeValue('data-langcode');
327     if (empty($langcode)) {
328       $langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
329     }
330     return $langcode;
331   }
332
333 }