You are here

function _menu_link_weight_get_tree in Menu Link Weight 8

Same name and namespace in other branches
  1. 8.2 menu_link_weight.module \_menu_link_weight_get_tree()
  2. 7 menu_link_weight.module \_menu_link_weight_get_tree()

Helper function to get all siblings of an item based on the parent.

Parameters

string $menu_name: The name of the menu.

string $parent_id: The parent link plugin ID.

Return value

\Drupal\Core\Menu\MenuLinkTreeElement[] A menu link tree.

1 call to _menu_link_weight_get_tree()
_menu_link_weight_get_options in ./menu_link_weight.module
Gets a list of of options for a specific menu/parent.

File

./menu_link_weight.module, line 181
Replaces the menu link weight dropdown with a tabledrag widget.

Code

function _menu_link_weight_get_tree($menu_name, $parent_id) {

  /** @var \Drupal\Core\Menu\MenuLinkTreeInterface $menu_link_tree */
  $menu_link_tree = \Drupal::service('menu.link_tree');
  if ($parent_id !== '') {

    /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
    $parent_tree = $menu_link_tree
      ->load($menu_name, (new MenuTreeParameters())
      ->addCondition('id', $parent_id));
    $parent_element = reset($parent_tree);
    $limit = $parent_element->depth + 1;
  }
  else {
    $limit = 1;
  }
  $tree = $menu_link_tree
    ->load($menu_name, (new MenuTreeParameters())
    ->setActiveTrail([
    $parent_id,
  ])
    ->setMinDepth($limit)
    ->setMaxDepth($limit)
    ->addCondition('parent', $parent_id));

  /** @see \Drupal\Core\Menu\MenuParentFormSelector::getParentSelectOptions() */
  $manipulators = array(
    array(
      'callable' => 'menu.default_tree_manipulators:checkNodeAccess',
    ),
    array(
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ),
    array(
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ),
  );
  return $menu_link_tree
    ->transform($tree, $manipulators);
}