3 namespace Drupal\entity_embed\EntityEmbedDisplay;
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;
17 * Defines a base Entity Embed Display implementation.
19 * @see \Drupal\entity_embed\Annotation\EntityEmbedDisplay
20 * @see \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayInterface
21 * @see \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager
24 * @ingroup entity_embed_api
26 abstract class EntityEmbedDisplayBase extends PluginBase implements ContainerFactoryPluginInterface, EntityEmbedDisplayInterface {
29 * The entity type manager service.
31 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
33 protected $entityTypeManager;
36 * The language manager.
38 * @var \Drupal\Core\Language\LanguageManagerInterface $language_manager
40 protected $languageManager;
43 * The context for the plugin.
47 public $context = array();
50 * The attributes on the embedded entity.
54 public $attributes = array();
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.
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;
74 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
79 $container->get('entity_type.manager'),
80 $container->get('language_manager')
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
91 return AccessResult::allowedIf($this->isValidEntityType())
92 // @see \Drupal\Core\Entity\EntityTypeManager
93 ->addCacheTags(['entity_types']);
97 * Validates that this Entity Embed Display plugin applies to the current
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.
105 * TRUE if the plugin can display the current entity type, or FALSE
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)) {
116 $definition = $this->getPluginDefinition();
117 if ($definition['entity_types'] === FALSE) {
121 return in_array($entity_type, $definition['entity_types']);
128 abstract public function build();
133 public function calculateDependencies() {
140 public function defaultConfiguration() {
147 public function getConfiguration() {
148 return $this->configuration;
154 public function setConfiguration(array $configuration) {
155 $this->configuration = NestedArray::mergeDeep(
156 $this->defaultConfiguration(),
165 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
172 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
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());
186 * Gets a configuration value.
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.
194 * The currently set configuration value, or the value of $default if the
195 * configuration value is not set.
197 public function getConfigurationValue($name, $default = NULL) {
198 $configuration = $this->getConfiguration();
199 return array_key_exists($name, $configuration) ? $configuration[$name] : $default;
203 * Sets the value for a defined context.
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.
211 public function setContextValue($name, $value) {
212 $this->context[$name] = $value;
216 * Gets the values for all defined contexts.
219 * An array of set context values, keyed by context name.
221 public function getContextValues() {
222 return $this->context;
226 * Gets the value for a defined context.
228 * @param string $name
229 * The name of the context in the plugin configuration.
232 * The currently set context value.
234 public function getContextValue($name) {
235 return $this->context[$name];
239 * Returns whether or not value is set for a defined context.
241 * @param string $name
242 * The name of the context in the plugin configuration.
245 * True if context value exists, false otherwise.
247 public function hasContextValue($name) {
248 return array_key_exists($name, $this->context);
252 * Gets the entity type from the current context.
255 * The entity type id.
257 public function getEntityTypeFromContext() {
258 if ($this->hasContextValue('entity')) {
259 return $this->getContextValue('entity')->getEntityTypeId();
262 return $this->getContextValue('entity_type');
267 * Gets the entity from the current context.
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.
276 * @return \Drupal\Core\Entity\EntityInterface
278 public function getEntityFromContext() {
279 if ($this->hasContextValue('entity')) {
280 return $this->getContextValue('entity');
285 * Sets the values for all attributes.
287 * @param array $attributes
288 * An array of attributes, keyed by attribute name.
290 public function setAttributes(array $attributes) {
291 $this->attributes = $attributes;
295 * Gets the values for all attributes.
298 * An array of set attribute values, keyed by attribute name.
300 public function getAttributeValues() {
301 return $this->attributes;
305 * Gets the value for an attribute.
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.
313 * The currently set attribute value.
315 public function getAttributeValue($name, $default = NULL) {
316 $attributes = $this->getAttributeValues();
317 return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
321 * Gets the current language code.
325 public function getLangcode() {
326 $langcode = $this->getAttributeValue('data-langcode');
327 if (empty($langcode)) {
328 $langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();