Pull merge.
[yaffs-website] / web / core / modules / menu_link_content / src / Plugin / Menu / MenuLinkContent.php
1 <?php
2
3 namespace Drupal\menu_link_content\Plugin\Menu;
4
5 use Drupal\Component\Plugin\Exception\PluginException;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Language\LanguageManagerInterface;
8 use Drupal\Core\Menu\MenuLinkBase;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Provides the menu link plugin for content menu links.
14  */
15 class MenuLinkContent extends MenuLinkBase implements ContainerFactoryPluginInterface {
16
17   /**
18    * Entities IDs to load.
19    *
20    * It is an array of entity IDs keyed by entity IDs.
21    *
22    * @var array
23    */
24   protected static $entityIdsToLoad = [];
25
26   /**
27    * {@inheritdoc}
28    */
29   protected $overrideAllowed = [
30     'menu_name' => 1,
31     'parent' => 1,
32     'weight' => 1,
33     'expanded' => 1,
34     'enabled' => 1,
35     'title' => 1,
36     'description' => 1,
37     'route_name' => 1,
38     'route_parameters' => 1,
39     'url' => 1,
40     'options' => 1,
41   ];
42
43   /**
44    * The menu link content entity connected to this plugin instance.
45    *
46    * @var \Drupal\menu_link_content\MenuLinkContentInterface
47    */
48   protected $entity;
49
50   /**
51    * The entity manager.
52    *
53    * @var \Drupal\Core\Entity\EntityManagerInterface
54    */
55   protected $entityManager;
56
57   /**
58    * The language manager.
59    *
60    * @var \Drupal\Core\Language\LanguageManagerInterface
61    */
62   protected $languageManager;
63
64   /**
65    * Constructs a new MenuLinkContent.
66    *
67    * @param array $configuration
68    *   A configuration array containing information about the plugin instance.
69    * @param string $plugin_id
70    *   The plugin_id for the plugin instance.
71    * @param mixed $plugin_definition
72    *   The plugin implementation definition.
73    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
74    *   The entity manager.
75    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
76    *   The language manager.
77    */
78   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager) {
79     parent::__construct($configuration, $plugin_id, $plugin_definition);
80
81     if (!empty($this->pluginDefinition['metadata']['entity_id'])) {
82       $entity_id = $this->pluginDefinition['metadata']['entity_id'];
83       // Builds a list of entity IDs to take advantage of the more efficient
84       // EntityStorageInterface::loadMultiple() in getEntity() at render time.
85       static::$entityIdsToLoad[$entity_id] = $entity_id;
86     }
87
88     $this->entityManager = $entity_manager;
89     $this->languageManager = $language_manager;
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
96     return new static(
97       $configuration,
98       $plugin_id,
99       $plugin_definition,
100       $container->get('entity.manager'),
101       $container->get('language_manager')
102     );
103   }
104
105   /**
106    * Loads the entity associated with this menu link.
107    *
108    * @return \Drupal\menu_link_content\MenuLinkContentInterface
109    *   The menu link content entity.
110    *
111    * @throws \Drupal\Component\Plugin\Exception\PluginException
112    *   If the entity ID and UUID are both invalid or missing.
113    */
114   protected function getEntity() {
115     if (empty($this->entity)) {
116       $entity = NULL;
117       $storage = $this->entityManager->getStorage('menu_link_content');
118       if (!empty($this->pluginDefinition['metadata']['entity_id'])) {
119         $entity_id = $this->pluginDefinition['metadata']['entity_id'];
120         // Make sure the current ID is in the list, since each plugin empties
121         // the list after calling loadMultiple(). Note that the list may include
122         // multiple IDs added earlier in each plugin's constructor.
123         static::$entityIdsToLoad[$entity_id] = $entity_id;
124         $entities = $storage->loadMultiple(array_values(static::$entityIdsToLoad));
125         $entity = isset($entities[$entity_id]) ? $entities[$entity_id] : NULL;
126         static::$entityIdsToLoad = [];
127       }
128       if (!$entity) {
129         // Fallback to the loading by the UUID.
130         $uuid = $this->getUuid();
131         $loaded_entities = $storage->loadByProperties(['uuid' => $uuid]);
132         $entity = reset($loaded_entities);
133       }
134       if (!$entity) {
135         throw new PluginException("Entity not found through the menu link plugin definition and could not fallback on UUID '$uuid'");
136       }
137       // Clone the entity object to avoid tampering with the static cache.
138       $this->entity = clone $entity;
139       $the_entity = $this->entityManager->getTranslationFromContext($this->entity);
140       /** @var \Drupal\menu_link_content\MenuLinkContentInterface $the_entity */
141       $this->entity = $the_entity;
142       $this->entity->setInsidePlugin();
143     }
144     return $this->entity;
145   }
146
147   /**
148    * {@inheritdoc}
149    */
150   public function getTitle() {
151     // We only need to get the title from the actual entity if it may be a
152     // translation based on the current language context. This can only happen
153     // if the site is configured to be multilingual.
154     if ($this->languageManager->isMultilingual()) {
155       return $this->getEntity()->getTitle();
156     }
157     return $this->pluginDefinition['title'];
158   }
159
160   /**
161    * {@inheritdoc}
162    */
163   public function getDescription() {
164     // We only need to get the description from the actual entity if it may be a
165     // translation based on the current language context. This can only happen
166     // if the site is configured to be multilingual.
167     if ($this->languageManager->isMultilingual()) {
168       return $this->getEntity()->getDescription();
169     }
170     return $this->pluginDefinition['description'];
171   }
172
173   /**
174    * {@inheritdoc}
175    */
176   public function getDeleteRoute() {
177     return $this->getEntity()->urlInfo('delete-form');
178   }
179
180   /**
181    * {@inheritdoc}
182    */
183   public function getEditRoute() {
184     return $this->getEntity()->urlInfo();
185   }
186
187   /**
188    * {@inheritdoc}
189    */
190   public function getTranslateRoute() {
191     return $this->getEntity()->urlInfo('drupal:content-translation-overview');
192   }
193
194   /**
195    * Returns the unique ID representing the menu link.
196    *
197    * @return string
198    *   The menu link ID.
199    */
200   protected function getUuid() {
201     return $this->getDerivativeId();
202   }
203
204   /**
205    * {@inheritdoc}
206    */
207   public function updateLink(array $new_definition_values, $persist) {
208     // Filter the list of updates to only those that are allowed.
209     $overrides = array_intersect_key($new_definition_values, $this->overrideAllowed);
210     // Update the definition.
211     $this->pluginDefinition = $overrides + $this->getPluginDefinition();
212     if ($persist) {
213       $entity = $this->getEntity();
214       foreach ($overrides as $key => $value) {
215         $entity->{$key}->value = $value;
216       }
217       $this->entityManager->getStorage('menu_link_content')->save($entity);
218     }
219
220     return $this->pluginDefinition;
221   }
222
223   /**
224    * {@inheritdoc}
225    */
226   public function isDeletable() {
227     return TRUE;
228   }
229
230   /**
231    * {@inheritdoc}
232    */
233   public function isTranslatable() {
234     return $this->getEntity()->isTranslatable();
235   }
236
237   /**
238    * {@inheritdoc}
239    */
240   public function deleteLink() {
241     $this->getEntity()->delete();
242   }
243
244 }