You are here

class MenuLinkTreeManipulators in Menu Manipulator 8

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

Provides a couple of menu link tree manipulators.

This class provides menu link tree manipulators to:

Hierarchy

Expanded class hierarchy of MenuLinkTreeManipulators

See also

menu_manipulator_get_multilingual_menu() To see an example of use.

1 string reference to 'MenuLinkTreeManipulators'
menu_manipulator.services.yml in ./menu_manipulator.services.yml
menu_manipulator.services.yml
1 service uses MenuLinkTreeManipulators
menu_manipulator.menu_tree_manipulators in ./menu_manipulator.services.yml
Drupal\menu_manipulator\Menu\MenuLinkTreeManipulators

File

src/Menu/MenuLinkTreeManipulators.php, line 27

Namespace

Drupal\menu_manipulator\Menu
View source
class MenuLinkTreeManipulators extends DefaultMenuLinkTreeManipulators {

  /**
   * The access manager.
   *
   * @var \Drupal\Core\Access\AccessManagerInterface
   */
  protected $accessManager;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity repository.
   *
   * @var \Drupal\Core\Entity\EntityRepository
   */
  protected $entityRepository;

  /**
   * The current language ID.
   *
   * @var string
   */
  protected $langcode;

  /**
   * The list of available languages.
   *
   * @var array
   *    \Drupal\Core\Language\LanguageInterface[] An associative array of
   *    languages keyed by the language code.
   */
  protected $languages;

  /**
   * Drupal\Core\Entity\EntityStorageInterface.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   *    A storage instance.
   */
  protected $menuLinkContentStorage;

  /**
   * Constructs a \Drupal\Core\Menu\DefaultMenuLinkTreeManipulators object.
   *
   * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
   *   The access manager.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current user.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Language\LanguageManager $language_manager
   *   The language manager.
   * @param \Drupal\Core\Entity\EntityRepository $entity_repository
   *   The entity repository.
   */
  public function __construct(AccessManagerInterface $access_manager, AccountInterface $account, EntityTypeManagerInterface $entity_type_manager, LanguageManager $language_manager, EntityRepository $entity_repository) {
    $this->accessManager = $access_manager;
    $this->account = $account;
    $this->entityTypeManager = $entity_type_manager;
    $this->entityRepository = $entity_repository;
    $this->langcode = $language_manager
      ->getCurrentLanguage()
      ->getId();
    $this->languages = $language_manager
      ->getLanguages();
    $this->menuLinkContentStorage = $this->entityTypeManager
      ->getStorage('menu_link_content');
  }

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

        // Filter also children items.
        if ($element->hasChildren) {
          $element->subtree = $this
            ->filterByCurrentLanguage($element->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) {
    $matadata = $link
      ->getMetaData();
    $loaded_link = $this->menuLinkContentStorage
      ->load($matadata['entity_id']);
    $loaded_lang_link = $this->entityRepository
      ->getTranslationFromContext($loaded_link);
    return $loaded_lang_link
      ->language()
      ->getId();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultMenuLinkTreeManipulators::checkAccess public function Performs access checks of a menu tree.
DefaultMenuLinkTreeManipulators::checkNodeAccess public function Performs access checking for nodes in an optimized way.
DefaultMenuLinkTreeManipulators::collectNodeLinks protected function Collects the node links in the menu tree.
DefaultMenuLinkTreeManipulators::flatten public function Flattens the tree to a single level.
DefaultMenuLinkTreeManipulators::generateIndexAndSort public function Generates a unique index and sorts by it.
DefaultMenuLinkTreeManipulators::menuLinkCheckAccess protected function Checks access for one menu link instance.
MenuLinkTreeManipulators::$accessManager protected property The access manager. Overrides DefaultMenuLinkTreeManipulators::$accessManager
MenuLinkTreeManipulators::$account protected property The current user. Overrides DefaultMenuLinkTreeManipulators::$account
MenuLinkTreeManipulators::$entityRepository protected property The entity repository.
MenuLinkTreeManipulators::$entityTypeManager protected property The entity type manager. Overrides DefaultMenuLinkTreeManipulators::$entityTypeManager
MenuLinkTreeManipulators::$langcode protected property The current language ID.
MenuLinkTreeManipulators::$languages protected property The list of available languages.
MenuLinkTreeManipulators::$menuLinkContentStorage protected property Drupal\Core\Entity\EntityStorageInterface.
MenuLinkTreeManipulators::filterByCurrentLanguage public function Filter a menu tree by current language MenuLinks.
MenuLinkTreeManipulators::getLinkLanguage protected function Force the MenuLinkContent to tell us its language code.
MenuLinkTreeManipulators::__construct public function Constructs a \Drupal\Core\Menu\DefaultMenuLinkTreeManipulators object. Overrides DefaultMenuLinkTreeManipulators::__construct