3 namespace Drupal\menu_link_content\Plugin\Menu;
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;
13 * Provides the menu link plugin for content menu links.
15 class MenuLinkContent extends MenuLinkBase implements ContainerFactoryPluginInterface {
18 * Entities IDs to load.
20 * It is an array of entity IDs keyed by entity IDs.
24 protected static $entityIdsToLoad = [];
29 protected $overrideAllowed = [
38 'route_parameters' => 1,
44 * The menu link content entity connected to this plugin instance.
46 * @var \Drupal\menu_link_content\MenuLinkContentInterface
53 * @var \Drupal\Core\Entity\EntityManagerInterface
55 protected $entityManager;
58 * The language manager.
60 * @var \Drupal\Core\Language\LanguageManagerInterface
62 protected $languageManager;
65 * Constructs a new MenuLinkContent.
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
75 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
76 * The language manager.
78 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager) {
79 parent::__construct($configuration, $plugin_id, $plugin_definition);
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;
88 $this->entityManager = $entity_manager;
89 $this->languageManager = $language_manager;
95 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
100 $container->get('entity.manager'),
101 $container->get('language_manager')
106 * Loads the entity associated with this menu link.
108 * @return \Drupal\menu_link_content\MenuLinkContentInterface
109 * The menu link content entity.
111 * @throws \Drupal\Component\Plugin\Exception\PluginException
112 * If the entity ID and UUID are both invalid or missing.
114 protected function getEntity() {
115 if (empty($this->entity)) {
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 = [];
129 // Fallback to the loading by the UUID.
130 $uuid = $this->getUuid();
131 $loaded_entities = $storage->loadByProperties(['uuid' => $uuid]);
132 $entity = reset($loaded_entities);
135 throw new PluginException("Entity not found through the menu link plugin definition and could not fallback on UUID '$uuid'");
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();
144 return $this->entity;
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();
157 return $this->pluginDefinition['title'];
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();
170 return $this->pluginDefinition['description'];
176 public function getDeleteRoute() {
177 return $this->getEntity()->urlInfo('delete-form');
183 public function getEditRoute() {
184 return $this->getEntity()->urlInfo();
190 public function getTranslateRoute() {
191 return $this->getEntity()->urlInfo('drupal:content-translation-overview');
195 * Returns the unique ID representing the menu link.
200 protected function getUuid() {
201 $this->getDerivativeId();
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();
213 $entity = $this->getEntity();
214 foreach ($overrides as $key => $value) {
215 $entity->{$key}->value = $value;
217 $this->entityManager->getStorage('menu_link_content')->save($entity);
220 return $this->pluginDefinition;
226 public function isDeletable() {
233 public function isTranslatable() {
234 return $this->getEntity()->isTranslatable();
240 public function deleteLink() {
241 $this->getEntity()->delete();