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

Correctly get entity from route match #3251675

- Fix an issue with taxonomy_term route
parent 44c00172
Branches
Tags
No related merge requests found
......@@ -7,4 +7,5 @@ services:
"@entity_type.manager",
"@language_manager",
"@config.factory",
"@router.no_access_checks",
]
......@@ -5,6 +5,7 @@ namespace Drupal\menu_manipulator\Menu;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepository;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
......@@ -12,6 +13,7 @@ use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Menu\InaccessibleMenuLink;
use Drupal\Core\Menu\MenuLinkBase;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\Router;
use Drupal\Core\Url;
/**
......@@ -52,6 +54,13 @@ class MenuLinkTreeManipulators {
*/
protected $config;
/**
* A router instance.
*
* @var \Drupal\Core\Routing\Router
*/
protected $router;
/**
* Constructs a \Drupal\Core\Menu\DefaultMenuLinkTreeManipulators object.
*
......@@ -63,17 +72,21 @@ class MenuLinkTreeManipulators {
* The entity repository.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @var \Drupal\Core\Routing\Router $router
* The router instance.
*/
public function __construct(
EntityRepository $entity_repository,
EntityTypeManagerInterface $entity_type_manager,
LanguageManager $language_manager,
ConfigFactoryInterface $config_factory
ConfigFactoryInterface $config_factory,
Router $router
) {
$this->entityRepository = $entity_repository;
$this->menuLinkContentStorage = $entity_type_manager->getStorage('menu_link_content');
$this->langcode = $language_manager->getCurrentLanguage()->getId();
$this->config = $config_factory->get('menu_manipulator.settings');
$this->router = $router;
}
/**
......@@ -205,26 +218,28 @@ class MenuLinkTreeManipulators {
}
$loaded_link = $this->menuLinkContentStorage->load($metadata['entity_id']);
$url = Url::fromUri($loaded_link->get('link')->getString());
$uri = $loaded_link->get('link')->getString();
$url = Url::fromUri($uri);
if (!$url instanceof Url || !$url->isRouted()) {
return FALSE;
}
// Get entity info from route.
$parts = explode('/', $url->getInternalPath());
if ((empty($parts[0] ?? '')) || (empty($parts[1] ?? ''))) {
return FALSE;
}
// Get core route parameters (e.g. /node/1).
$entity_type = $parts[0] ?? NULL;
$entity_id = $parts[1] ?? NULL;
if (!\is_string($entity_type) || !$entity_id) {
return FALSE;
// @see https://www.drupal.org/project/menu_manipulator/issues/3251675
// @see https://www.computerminds.co.uk/drupal-code/get-entity-route
$route_match = $this->router->match($uri);
if ($route = $route_match['_route_object'] ?? NULL) {
foreach ($route->getOption('parameters') as $name => $options) {
if (isset($options['type']) && strpos($options['type'], 'entity:') === 0) {
$entity = $route_match[$name] ?? NULL;
if ($entity instanceof EntityInterface) {
return $this->entityRepository->getActive($entity->getEntityTypeId(), $entity->id());
}
}
}
}
return $this->entityRepository->getActive($entity_type, $entity_id);
return FALSE;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment