MenuLinkTreeManipulators.php in Menu Manipulator 2.0.x
File
src/Menu/MenuLinkTreeManipulators.php
View source
<?php
namespace Drupal\menu_manipulator\Menu;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityRepository;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Menu\InaccessibleMenuLink;
use Drupal\Core\Menu\MenuLinkBase;
use Drupal\Core\Url;
class MenuLinkTreeManipulators {
protected $entityRepository;
protected $langcode;
protected $menuLinkContentStorage;
public function __construct(EntityRepository $entity_repository, EntityTypeManagerInterface $entity_type_manager, LanguageManager $language_manager) {
$this->entityRepository = $entity_repository;
$this->menuLinkContentStorage = $entity_type_manager
->getStorage('menu_link_content');
$this->langcode = $language_manager
->getCurrentLanguage()
->getId();
}
public function filterByCurrentLanguage(array $tree) {
foreach ($tree as $key => $element) {
if (!$element->link instanceof MenuLinkBase) {
continue;
}
$access = $this
->checkLinkAccess($element->link);
if (!$access) {
$tree[$key]->link = new InaccessibleMenuLink($tree[$key]->link);
$tree[$key]->access = AccessResult::forbidden();
$tree[$key]->subtree = [];
}
if ($element->hasChildren && !empty($tree[$key]->subtree)) {
$element->subtree = $this
->filterByCurrentLanguage($element->subtree);
}
}
return $tree;
}
public function checkLinkAccess(MenuLinkBase $link) {
$langcode = $this
->getLinkLanguage($link);
$not_applicable_langcodes = [
LanguageInterface::LANGCODE_NOT_APPLICABLE,
LanguageInterface::LANGCODE_NOT_SPECIFIED,
];
if (in_array($langcode, $not_applicable_langcodes)) {
return TRUE;
}
$entity = $this
->getLinkEntity($link);
if ($entity instanceof ContentEntityInterface && method_exists($entity, 'hasTranslation')) {
return $entity
->hasTranslation($this->langcode);
}
return $this->langcode == $langcode;
}
protected function getLinkLanguage(MenuLinkBase $link) {
$metadata = $link
->getMetaData();
if (!isset($metadata['entity_id'])) {
return LanguageInterface::LANGCODE_NOT_APPLICABLE;
}
$loaded_link = $this->menuLinkContentStorage
->load($metadata['entity_id']);
$loaded_lang_link = $this->entityRepository
->getTranslationFromContext($loaded_link);
return $loaded_lang_link
->language()
->getId();
}
protected function getLinkEntity(MenuLinkBase $link) {
$metadata = $link
->getMetaData();
if (!isset($metadata['entity_id'])) {
return NULL;
}
$loaded_link = $this->menuLinkContentStorage
->load($metadata['entity_id']);
$url = Url::fromUri($loaded_link
->get('link')
->getString());
if (!$url instanceof Url || !$url
->isRouted()) {
return FALSE;
}
$parts = explode('/', $url
->getInternalPath());
if (empty($parts[0] ?? '') || empty($parts[1] ?? '')) {
return FALSE;
}
$entity_type = $parts[0];
$entity_id = $parts[1];
return !\is_string($entity_type) || empty($entity_id) ? FALSE : $this->entityRepository
->getActive($entity_type, $entity_id);
}
}