You are here

class UltimenuTree in Ultimenu 8.2

Provides Ultimenu utility methods.

Hierarchy

Expanded class hierarchy of UltimenuTree

1 string reference to 'UltimenuTree'
ultimenu.services.yml in ./ultimenu.services.yml
ultimenu.services.yml
1 service uses UltimenuTree
ultimenu.tree in ./ultimenu.services.yml
Drupal\ultimenu\UltimenuTree

File

src/UltimenuTree.php, line 16

Namespace

Drupal\ultimenu
View source
class UltimenuTree implements UltimenuTreeInterface {
  use StringTranslationTrait;

  /**
   * The menu link tree manager.
   *
   * @var \Drupal\Core\Menu\MenuLinkTreeInterface
   */
  protected $menuTree;

  /**
   * The active menu trail service.
   *
   * @var \Drupal\Core\Menu\MenuActiveTrailInterface
   */
  protected $menuActiveTrail;

  /**
   * Constructs a UltimenuTree object.
   */
  public function __construct(MenuLinkTreeInterface $menu_tree, MenuActiveTrailInterface $menu_active_trail) {
    $this->menuTree = $menu_tree;
    $this->menuActiveTrail = $menu_active_trail;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('menu.link_tree'), $container
      ->get('menu.active_trail'));
  }

  /**
   * Returns the menu tree.
   */
  public function getMenuTree() {
    return $this->menuTree;
  }

  /**
   * Returns the menu active trail.
   */
  public function getMenuActiveTrail() {
    return $this->menuActiveTrail;
  }

  /**
   * {@inheritdoc}
   */
  public function getMenus() {
    $custom_menus = [];
    if ($menus = Menu::loadMultiple()) {
      foreach ($menus as $menu_name => $menu) {
        $custom_menus[$menu_name] = Html::escape($menu
          ->label());
      }
    }
    $excluded_menus = [
      'admin' => $this
        ->t('Administration'),
      'devel' => $this
        ->t('Development'),
      'tools' => $this
        ->t('Tools'),
    ];
    $options = array_diff_key($custom_menus, $excluded_menus);
    asort($options);
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function loadMenuTree($menu_name) {
    $parameters = new MenuTreeParameters();
    $parameters
      ->setTopLevelOnly()
      ->onlyEnabledLinks();
    $tree = $this->menuTree
      ->load($menu_name, $parameters);
    $manipulators = [
      [
        'callable' => 'menu.default_tree_manipulators:checkAccess',
      ],
      [
        'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
      ],
      [
        'callable' => 'menu.default_tree_manipulators:flatten',
      ],
    ];
    $tree = $this->menuTree
      ->transform($tree, $manipulators);
    return $tree;
  }

  /**
   * {@inheritdoc}
   */
  public function loadSubMenuTree($menu_name, $link_id, $title = '') {
    $build = [];
    $level = 1;
    $depth = 4;
    $parameters = $this->menuTree
      ->getCurrentRouteMenuTreeParameters($menu_name);
    $parameters
      ->setRoot($link_id)
      ->excludeRoot()
      ->onlyEnabledLinks();
    $parameters
      ->setMaxDepth(min($level + $depth - 1, $this->menuTree
      ->maxDepth()));
    $tree = $this->menuTree
      ->load($menu_name, $parameters);
    $manipulators = [
      [
        'callable' => 'menu.default_tree_manipulators:checkAccess',
      ],
      [
        'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
      ],
    ];
    $tree = $this->menuTree
      ->transform($tree, $manipulators);
    if ($tree) {
      $build['content'] = $this->menuTree
        ->build($tree);
      $css_name = Html::cleanCssIdentifier(mb_strtolower($menu_name . '-' . $title));
      $build['#attributes']['class'] = [
        'ultimenusub',
        'ultimenusub--' . $css_name,
      ];
      $build['#theme_wrappers'][] = 'container';
    }
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UltimenuTree::$menuActiveTrail protected property The active menu trail service.
UltimenuTree::$menuTree protected property The menu link tree manager.
UltimenuTree::create public static function
UltimenuTree::getMenuActiveTrail public function Returns the menu active trail.
UltimenuTree::getMenus public function
UltimenuTree::getMenuTree public function Returns the menu tree.
UltimenuTree::loadMenuTree public function Returns a list of links based on the menu name. Overrides UltimenuTreeInterface::loadMenuTree
UltimenuTree::loadSubMenuTree public function Returns a list of submenu links based on the menu name. Overrides UltimenuTreeInterface::loadSubMenuTree
UltimenuTree::__construct public function Constructs a UltimenuTree object.