MenuLinkTreeInterface.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Drupal\Core\Menu;
  3. /**
  4. * Defines an interface for loading, transforming and rendering menu link trees.
  5. *
  6. * The main purposes of this interface are:
  7. * - Load a list of menu links, given a menu name, using
  8. * MenuLinkTreeInterface::load(). Loaded menu links are returned as a
  9. * tree by looking at the links' tree meta-data.
  10. * - Which links are loaded can be specified in the menu link tree parameters
  11. * that are passed to the load() method. You can build your own set of
  12. * parameters, or you can start from typical defaults by calling the
  13. * MenuLinkTreeInterface::getCurrentRouteMenuTreeParameters() method. See
  14. * \Drupal\Core\Menu\MenuTreeParameters for more on menu tree parameters.
  15. * - Transform a menu link tree, by calling MenuLinkTreeInterface::transform().
  16. * Examples include access checking, adding custom classes, extracting a
  17. * subtree depending on the active trail, etc. Note that translation is not
  18. * a tree transformation, because menu links themselves are responsible
  19. * for translation. Transformations are performed by "menu link tree
  20. * manipulators", which are functions or methods; see
  21. * \Drupal\Core\Menu\DefaultMenuLinkTreeManipulators for examples.
  22. * - Create a render array using MenuLinkTreeInterface::build().
  23. */
  24. interface MenuLinkTreeInterface {
  25. /**
  26. * Gets the link tree parameters for rendering a specific menu.
  27. *
  28. * Builds menu link tree parameters that:
  29. * - Expand all links in the active trail based on route being viewed.
  30. * - Expand the descendants of the links in the active trail whose
  31. * 'expanded' flag is enabled.
  32. *
  33. * This only sets the (relatively complex) parameters to achieve the two above
  34. * goals, but you can still further customize these parameters.
  35. *
  36. * @param string $menu_name
  37. * The menu name, needed for retrieving the active trail and links with the
  38. * 'expanded' flag enabled.
  39. *
  40. * @return \Drupal\Core\Menu\MenuTreeParameters
  41. * The parameters to determine which menu links to be loaded into a tree.
  42. *
  43. * @see \Drupal\Core\Menu\MenuTreeParameters
  44. */
  45. public function getCurrentRouteMenuTreeParameters($menu_name);
  46. /**
  47. * Loads a menu tree with a menu link plugin instance at each element.
  48. *
  49. * @param string $menu_name
  50. * The name of the menu.
  51. * @param \Drupal\Core\Menu\MenuTreeParameters $parameters
  52. * The parameters to determine which menu links to be loaded into a tree.
  53. *
  54. * @return \Drupal\Core\Menu\MenuLinkTreeElement[]
  55. * A menu link tree.
  56. */
  57. public function load($menu_name, MenuTreeParameters $parameters);
  58. /**
  59. * Applies menu link tree manipulators to transform the given tree.
  60. *
  61. * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree
  62. * The menu tree to manipulate.
  63. * @param array $manipulators
  64. * The menu link tree manipulators to apply. Each is an array with keys:
  65. * - callable: a callable or a string that can be resolved to a callable
  66. * by \Drupal\Core\Controller\ControllerResolverInterface::getControllerFromDefinition()
  67. * - args: optional array of arguments to pass to the callable after $tree.
  68. *
  69. * @return \Drupal\Core\Menu\MenuLinkTreeElement[]
  70. * The manipulated menu link tree.
  71. */
  72. public function transform(array $tree, array $manipulators);
  73. /**
  74. * Builds a renderable array from a menu tree.
  75. *
  76. * The menu item's LI element is given one of the following classes:
  77. * - expanded: The menu item is showing its submenu.
  78. * - collapsed: The menu item has a submenu that is not shown.
  79. * - leaf: The menu item has no submenu.
  80. *
  81. * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree
  82. * A data structure representing the tree, as returned from
  83. * MenuLinkTreeInterface::load().
  84. *
  85. * @return array
  86. * A renderable array.
  87. */
  88. public function build(array $tree);
  89. /**
  90. * Returns the maximum depth of tree that is supported.
  91. *
  92. * @return int
  93. * The maximum depth.
  94. */
  95. public function maxDepth();
  96. /**
  97. * Finds the height of a subtree rooted by of the given ID.
  98. *
  99. * @param string $id
  100. * The ID of an item in the storage.
  101. *
  102. * @return int
  103. * Returns the height of the subtree. This will be at least 1 if the ID
  104. * exists, or 0 if the ID does not exist in the storage.
  105. */
  106. public function getSubtreeHeight($id);
  107. /**
  108. * Finds expanded links in a menu given a set of possible parents.
  109. *
  110. * @param string $menu_name
  111. * The menu name.
  112. * @param array $parents
  113. * One or more parent IDs to match.
  114. *
  115. * @return array
  116. * The menu link IDs that are flagged as expanded in this menu.
  117. */
  118. public function getExpanded($menu_name, array $parents);
  119. }