You are here

public function MenuLinkTreeManipulators::filterByCurrentLanguage in Menu Manipulator 8

Same name and namespace in other branches
  1. 8.2 src/Menu/MenuLinkTreeManipulators.php \Drupal\menu_manipulator\Menu\MenuLinkTreeManipulators::filterByCurrentLanguage()
  2. 3.0.x src/Menu/MenuLinkTreeManipulators.php \Drupal\menu_manipulator\Menu\MenuLinkTreeManipulators::filterByCurrentLanguage()
  3. 2.0.x src/Menu/MenuLinkTreeManipulators.php \Drupal\menu_manipulator\Menu\MenuLinkTreeManipulators::filterByCurrentLanguage()

Filter a menu tree by current language MenuLinks.

Parameters

\Drupal\Core\Menu\MenuLinkTreeElement[] $tree: The menu link tree to manipulate.

Return value

\Drupal\Core\Menu\MenuLinkTreeElement[] The manipulated menu link tree.

File

src/Menu/MenuLinkTreeManipulators.php, line 120

Class

MenuLinkTreeManipulators
Provides a couple of menu link tree manipulators.

Namespace

Drupal\menu_manipulator\Menu

Code

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 = [];
      }

      // Filter also children items.
      if ($element->hasChildren) {
        $element->subtree = $this
          ->filterByCurrentLanguage($element->subtree);
      }
    }
  }
  return $tree;
}