Skip to content
Snippets Groups Projects
Commit 4ab64263 authored by Matthieu Scarset's avatar Matthieu Scarset
Browse files

Check menu_link_content storage exists #3251578

parent f6b1dfd9
No related branches found
No related tags found
No related merge requests found
...@@ -26,6 +26,13 @@ use Drupal\Core\Url; ...@@ -26,6 +26,13 @@ use Drupal\Core\Url;
*/ */
class MenuLinkTreeManipulators { class MenuLinkTreeManipulators {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/** /**
* The entity repository. * The entity repository.
* *
...@@ -40,13 +47,6 @@ class MenuLinkTreeManipulators { ...@@ -40,13 +47,6 @@ class MenuLinkTreeManipulators {
*/ */
protected $langcode; protected $langcode;
/**
* The menu_link_content storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $menuLinkContentStorage;
/** /**
* Our custom configuration. * Our custom configuration.
* *
...@@ -83,7 +83,7 @@ class MenuLinkTreeManipulators { ...@@ -83,7 +83,7 @@ class MenuLinkTreeManipulators {
Router $router Router $router
) { ) {
$this->entityRepository = $entity_repository; $this->entityRepository = $entity_repository;
$this->menuLinkContentStorage = $entity_type_manager->getStorage('menu_link_content'); $this->entityTypeManager = $entity_type_manager;
$this->langcode = $language_manager->getCurrentLanguage()->getId(); $this->langcode = $language_manager->getCurrentLanguage()->getId();
$this->config = $config_factory->get('menu_manipulator.settings'); $this->config = $config_factory->get('menu_manipulator.settings');
$this->router = $router; $this->router = $router;
...@@ -117,8 +117,8 @@ class MenuLinkTreeManipulators { ...@@ -117,8 +117,8 @@ class MenuLinkTreeManipulators {
if ($element->hasChildren && !empty($tree[$key]->subtree)) { if ($element->hasChildren && !empty($tree[$key]->subtree)) {
$element->subtree = $this->filterTreeByCurrentLanguage($element->subtree); $element->subtree = $this->filterTreeByCurrentLanguage($element->subtree);
} }
} }
return $tree; return $tree;
} }
...@@ -182,20 +182,23 @@ class MenuLinkTreeManipulators { ...@@ -182,20 +182,23 @@ class MenuLinkTreeManipulators {
* Force the MenuLinkBase to tell us its language code. * Force the MenuLinkBase to tell us its language code.
* *
* @param \Drupal\Core\Menu\MenuLinkBase $link * @param \Drupal\Core\Menu\MenuLinkBase $link
* `The Menu Link Content entity. * The Menu Link item - usually an menu_link_content entity but it can be a
* config from Views or something else we don't even know about yet.
* *
* @return string * @return string
* The menu Link language ID or a default value. * The menu Link language ID or a default value.
*
* @todo Handle config links such as those added by Views (e.g. get language).
*/ */
protected function getLinkLanguage(MenuLinkBase $link) { protected function getLinkLanguage(MenuLinkBase $link) {
$metadata = $link->getMetaData(); $metadata = $link->getMetaData();
if (!isset($metadata['entity_id'])) { $entity_id = $metadata['entity_id'] ?? NULL;
return LanguageInterface::LANGCODE_NOT_APPLICABLE;
}
if ($loaded_link = $this->menuLinkContentStorage->load($metadata['entity_id'])) { if ($entity_id && $this->entityTypeManager->hasHandler('menu_link_content', 'storage')) {
if ($loaded_lang_link = $this->entityRepository->getTranslationFromContext($loaded_link)) { if ($loaded_link = $this->entityTypeManager->getStorage('menu_link_content')->load($entity_id)) {
return $loaded_lang_link->language()->getId(); if ($loaded_lang_link = $this->entityRepository->getTranslationFromContext($loaded_link)) {
return $loaded_lang_link->language()->getId();
}
} }
} }
...@@ -217,11 +220,16 @@ class MenuLinkTreeManipulators { ...@@ -217,11 +220,16 @@ class MenuLinkTreeManipulators {
return NULL; return NULL;
} }
$loaded_link = $this->menuLinkContentStorage->load($metadata['entity_id']); if ($this->entityTypeManager->hasHandler('menu_link_content', 'storage')) {
$uri = $loaded_link->get('link')->getString(); /** @var \Drupal\Core\Menu\MenuLinkInterface $loaded_link */
$url = Url::fromUri($uri); $loaded_link = $this->entityTypeManager->getStorage('menu_link_content')
if (!$url instanceof Url || !$url->isRouted()) { ->load($metadata['entity_id']);
return FALSE; $uri = $loaded_link->get('link')->getString();
$url = Url::fromUri($uri);
if (!$url instanceof Url || !$url->isRouted()) {
return FALSE;
}
} }
try { try {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment