accessManager = $access_manager; $this->account = $account; $this->entityTypeManager = $entity_type_manager; $this->langcode = $language_manager->getCurrentLanguage()->getId(); $this->languages = $language_manager->getLanguages(); } /** * Filter a menu tree by current language MenuLinks. * * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree * The menu link tree to manipulate. * * @return \Drupal\Core\Menu\MenuLinkTreeElement[] * The manipulated menu link tree. */ public function filterByCurrentLanguage(array $tree) { foreach ($tree as $key => $element) { if ($element->link instanceof MenuLinkContent) { // Shortcut to the MenuLink item. $link = &$element->link; // Get the MenuLinkContent entity language. $link->langcode = $this->getLinkLanguage($link); // Test if the MenuLinkContent is the same language as the current. if ($this->langcode == $link->langcode) { $tree[$key]->access = parent::menuLinkCheckAccess($element->link); } else { $tree[$key]->link = new InaccessibleMenuLink($tree[$key]->link); $tree[$key]->access = AccessResult::forbidden(); $tree[$key]->subtree = []; } } } return $tree; } /** * Force the MenuLinkContent to tell us its language code. * * @param \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent $link * `The Menu Link Content entity. * * @return string * The Menu Link Content entity's language ID. */ protected function getLinkLanguage(MenuLinkContent $link) { // Bypass protected function. // @see https://stackoverflow.com/a/44361579/6180339 $bypass_function = function () { return $this->getEntity()->language()->getId(); }; return $bypass_function->call($link); } }