MenuTreeStorageInterface.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace Drupal\Core\Menu;
  3. /**
  4. * Defines an interface for storing a menu tree containing menu link IDs.
  5. *
  6. * The tree contains a hierarchy of menu links which have an ID as well as a
  7. * route name or external URL.
  8. */
  9. interface MenuTreeStorageInterface {
  10. /**
  11. * The maximum depth of tree the storage implementation supports.
  12. *
  13. * @return int
  14. * The maximum depth.
  15. */
  16. public function maxDepth();
  17. /**
  18. * Clears all definitions cached in memory.
  19. */
  20. public function resetDefinitions();
  21. /**
  22. * Rebuilds the stored menu link definitions.
  23. *
  24. * Links that saved by passing definitions into this method must be included
  25. * on all future calls, or they will be purged. This allows for automatic
  26. * cleanup e.g. when modules are uninstalled.
  27. *
  28. * @param array $definitions
  29. * The new menu link definitions.
  30. */
  31. public function rebuild(array $definitions);
  32. /**
  33. * Loads a menu link plugin definition from the storage.
  34. *
  35. * @param string $id
  36. * The menu link plugin ID.
  37. *
  38. * @return array|false
  39. * The plugin definition, or FALSE if no definition was found for the ID.
  40. */
  41. public function load($id);
  42. /**
  43. * Loads multiple plugin definitions from the storage.
  44. *
  45. * @param array $ids
  46. * An array of plugin IDs.
  47. *
  48. * @return array
  49. * An array of plugin definition arrays keyed by plugin ID, which are the
  50. * actual definitions after the loadMultiple() including all those plugins
  51. * from $ids.
  52. */
  53. public function loadMultiple(array $ids);
  54. /**
  55. * Loads multiple plugin definitions from the storage based on properties.
  56. *
  57. * @param array $properties
  58. * The properties to filter by.
  59. *
  60. * @return array
  61. * An array of menu link definition arrays.
  62. *
  63. * @throws \InvalidArgumentException
  64. * Thrown if an invalid property name is specified in $properties.
  65. */
  66. public function loadByProperties(array $properties);
  67. /**
  68. * Loads multiple plugin definitions from the storage based on route.
  69. *
  70. * @param string $route_name
  71. * The route name.
  72. * @param array $route_parameters
  73. * (optional) The route parameters. Defaults to an empty array.
  74. * @param string $menu_name
  75. * (optional) Restricts the found links to just those in the named menu.
  76. *
  77. * @return array
  78. * An array of menu link definitions keyed by ID and ordered by depth.
  79. */
  80. public function loadByRoute($route_name, array $route_parameters = [], $menu_name = NULL);
  81. /**
  82. * Saves a plugin definition to the storage.
  83. *
  84. * @param array $definition
  85. * A definition for a \Drupal\Core\Menu\MenuLinkInterface plugin.
  86. *
  87. * @return array
  88. * The menu names affected by the save operation. This will be one menu
  89. * name if the link is saved to the sane menu, or two if it is saved to a
  90. * new menu.
  91. *
  92. * @throws \Exception
  93. * Thrown if the storage back-end does not exist and could not be created.
  94. * @throws \Drupal\Component\Plugin\Exception\PluginException
  95. * Thrown if the definition is invalid - for example, if the specified
  96. * parent would cause the links children to be moved to greater than the
  97. * maximum depth.
  98. */
  99. public function save(array $definition);
  100. /**
  101. * Deletes a menu link definition from the storage.
  102. *
  103. * @param string $id
  104. * The menu link plugin ID.
  105. */
  106. public function delete($id);
  107. /**
  108. * Loads a menu link tree from the storage.
  109. *
  110. * This function may be used build the data for a menu tree only, for example
  111. * to further massage the data manually before further processing happens.
  112. * MenuLinkTree::checkAccess() needs to be invoked afterwards.
  113. *
  114. * The tree order is maintained using an optimized algorithm, for example by
  115. * storing each parent in an individual field, see
  116. * https://www.drupal.org/node/141866 for more details. However, any details
  117. * of the storage should not be relied upon since it may be swapped with a
  118. * different implementation.
  119. *
  120. * @param string $menu_name
  121. * The name of the menu.
  122. * @param \Drupal\Core\Menu\MenuTreeParameters $parameters
  123. * The parameters to determine which menu links to be loaded into a tree.
  124. *
  125. * @return array
  126. * An array with 2 elements:
  127. * - tree: A fully built menu tree containing an array.
  128. * @see static::treeDataRecursive()
  129. * - route_names: An array of all route names used in the tree.
  130. */
  131. public function loadTreeData($menu_name, MenuTreeParameters $parameters);
  132. /**
  133. * Loads all the enabled menu links that are below the given ID.
  134. *
  135. * The returned links are not ordered, and visible children will be included
  136. * even if they have parent that is not enabled or ancestor so would not
  137. * normally appear in a rendered tree.
  138. *
  139. * @param string $id
  140. * The parent menu link ID.
  141. * @param int $max_relative_depth
  142. * The maximum relative depth of the children relative to the passed parent.
  143. *
  144. * @return array
  145. * An array of enabled link definitions, keyed by ID.
  146. */
  147. public function loadAllChildren($id, $max_relative_depth = NULL);
  148. /**
  149. * Loads all the IDs for menu links that are below the given ID.
  150. *
  151. * @param string $id
  152. * The parent menu link ID.
  153. *
  154. * @return array
  155. * An unordered array of plugin IDs corresponding to all children.
  156. */
  157. public function getAllChildIds($id);
  158. /**
  159. * Loads a subtree rooted by the given ID.
  160. *
  161. * The returned links are structured like those from loadTreeData().
  162. *
  163. * @param string $id
  164. * The menu link plugin ID.
  165. * @param int $max_relative_depth
  166. * (optional) The maximum depth of child menu links relative to the passed
  167. * in. Defaults to NULL, in which case the full subtree will be returned.
  168. *
  169. * @return array
  170. * An array with 2 elements:
  171. * - subtree: A fully built menu tree element or FALSE.
  172. * - route_names: An array of all route names used in the subtree.
  173. */
  174. public function loadSubtreeData($id, $max_relative_depth = NULL);
  175. /**
  176. * Returns all the IDs that represent the path to the root of the tree.
  177. *
  178. * @param string $id
  179. * A menu link ID.
  180. *
  181. * @return array
  182. * An associative array of IDs with keys equal to values that represents the
  183. * path from the given ID to the root of the tree. If $id is an ID that
  184. * exists, the returned array will at least include it. An empty array is
  185. * returned if the ID does not exist in the storage. An example $id (8) with
  186. * two parents (1, 6) looks like the following:
  187. * @code
  188. * array(
  189. * 'p1' => 1,
  190. * 'p2' => 6,
  191. * 'p3' => 8,
  192. * 'p4' => 0,
  193. * 'p5' => 0,
  194. * 'p6' => 0,
  195. * 'p7' => 0,
  196. * 'p8' => 0,
  197. * 'p9' => 0
  198. * )
  199. * @endcode
  200. */
  201. public function getRootPathIds($id);
  202. /**
  203. * Finds expanded links in a menu given a set of possible parents.
  204. *
  205. * @param string $menu_name
  206. * The menu name.
  207. * @param array $parents
  208. * One or more parent IDs to match.
  209. *
  210. * @return array
  211. * The menu link IDs that are flagged as expanded in this menu.
  212. */
  213. public function getExpanded($menu_name, array $parents);
  214. /**
  215. * Finds the height of a subtree rooted by the given ID.
  216. *
  217. * @param string $id
  218. * The ID of an item in the storage.
  219. *
  220. * @return int
  221. * Returns the height of the subtree. This will be at least 1 if the ID
  222. * exists, or 0 if the ID does not exist in the storage.
  223. */
  224. public function getSubtreeHeight($id);
  225. /**
  226. * Determines whether a specific menu name is used in the tree.
  227. *
  228. * @param string $menu_name
  229. * The menu name.
  230. *
  231. * @return bool
  232. * Returns TRUE if the given menu name is used, otherwise FALSE.
  233. */
  234. public function menuNameInUse($menu_name);
  235. /**
  236. * Returns the used menu names in the tree storage.
  237. *
  238. * @return array
  239. * The menu names.
  240. */
  241. public function getMenuNames();
  242. /**
  243. * Counts the total number of menu links in one menu or all menus.
  244. *
  245. * @param string $menu_name
  246. * (optional) The menu name to count by. Defaults to all menus.
  247. *
  248. * @return int
  249. * The number of menu links in the named menu, or in all menus if the menu
  250. * name is NULL.
  251. */
  252. public function countMenuLinks($menu_name = NULL);
  253. }