PageRenderTime 23ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/web/core/lib/Drupal/Core/Menu/MenuTreeParameters.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 259 lines | 80 code | 24 blank | 155 comment | 2 complexity | 61808b353589a71f5e72538a61a90702 MD5 | raw file
  1. <?php
  2. namespace Drupal\Core\Menu;
  3. /**
  4. * Provides a value object to model menu tree parameters.
  5. *
  6. * Menu tree parameters are used to determine the set of definitions to be
  7. * loaded from \Drupal\Core\Menu\MenuTreeStorageInterface. Hence they determine
  8. * the shape and content of the tree:
  9. * - Which parent IDs should be used to restrict the tree. Only links with
  10. * a parent in the list will be included.
  11. * - Which menu links are omitted, depending on the minimum and maximum depth.
  12. */
  13. class MenuTreeParameters implements \Serializable {
  14. /**
  15. * A menu link plugin ID that should be used as the root.
  16. *
  17. * By default the root ID of empty string '' is used. However, when only the
  18. * descendants (subtree) of a certain menu link are needed, a custom root can
  19. * be specified.
  20. *
  21. * @var string
  22. */
  23. public $root = '';
  24. /**
  25. * The minimum depth of menu links in the resulting tree relative to the root.
  26. *
  27. * @var int|null
  28. */
  29. public $minDepth = NULL;
  30. /**
  31. * The maximum depth of menu links in the resulting tree relative to the root.
  32. *
  33. * @var int|null
  34. */
  35. public $maxDepth = NULL;
  36. /**
  37. * An array of parent link IDs.
  38. *
  39. * This restricts the tree to only menu links that are at the top level or
  40. * have a parent ID in this list. If empty, the whole menu tree is built.
  41. *
  42. * @var string[]
  43. */
  44. public $expandedParents = [];
  45. /**
  46. * The IDs from the currently active menu link to the root of the whole tree.
  47. *
  48. * This is an array of menu link plugin IDs, representing the trail from the
  49. * currently active menu link to the ("real") root of that menu link's menu.
  50. * This does not affect the way the tree is built. It is only used to set the
  51. * value of the inActiveTrail property for each tree element.
  52. *
  53. * @var string[]
  54. */
  55. public $activeTrail = [];
  56. /**
  57. * The conditions used to restrict which links are loaded.
  58. *
  59. * An associative array of custom query condition key/value pairs.
  60. *
  61. * @var array
  62. */
  63. public $conditions = [];
  64. /**
  65. * Sets a root for menu tree loading.
  66. *
  67. * @param string $root
  68. * A menu link plugin ID, or empty string '' to use the root of the whole
  69. * tree.
  70. *
  71. * @return $this
  72. *
  73. * @codeCoverageIgnore
  74. */
  75. public function setRoot($root) {
  76. $this->root = (string) $root;
  77. return $this;
  78. }
  79. /**
  80. * Sets a minimum depth for menu tree loading.
  81. *
  82. * @param int $min_depth
  83. * The (root-relative) minimum depth to apply.
  84. *
  85. * @return $this
  86. */
  87. public function setMinDepth($min_depth) {
  88. $this->minDepth = max(1, $min_depth);
  89. return $this;
  90. }
  91. /**
  92. * Sets a maximum depth for menu tree loading.
  93. *
  94. * @param int $max_depth
  95. * The (root-relative) maximum depth to apply.
  96. *
  97. * @return $this
  98. *
  99. * @codeCoverageIgnore
  100. */
  101. public function setMaxDepth($max_depth) {
  102. $this->maxDepth = $max_depth;
  103. return $this;
  104. }
  105. /**
  106. * Adds parent menu links IDs to restrict the tree.
  107. *
  108. * @param string[] $parents
  109. * An array containing parent IDs. If supplied, the tree is limited to
  110. * links that have these parents.
  111. *
  112. * @return $this
  113. */
  114. public function addExpandedParents(array $parents) {
  115. $this->expandedParents = array_merge($this->expandedParents, $parents);
  116. $this->expandedParents = array_unique($this->expandedParents);
  117. return $this;
  118. }
  119. /**
  120. * Sets the active trail IDs used to set the inActiveTrail property.
  121. *
  122. * @param string[] $active_trail
  123. * An array containing the active trail: a list of menu link plugin IDs.
  124. *
  125. * @return $this
  126. *
  127. * @see \Drupal\Core\Menu\MenuActiveTrail::getActiveTrailIds()
  128. *
  129. * @codeCoverageIgnore
  130. */
  131. public function setActiveTrail(array $active_trail) {
  132. $this->activeTrail = $active_trail;
  133. return $this;
  134. }
  135. /**
  136. * Adds a custom query condition.
  137. *
  138. * @param string $definition_field
  139. * Only conditions that are testing menu link definition fields are allowed.
  140. * @param mixed $value
  141. * The value to test the link definition field against. In most cases, this
  142. * is a scalar. For more complex options, it is an array. The meaning of
  143. * each element in the array is dependent on the $operator.
  144. * @param string|null $operator
  145. * (optional) The comparison operator, such as =, <, or >=. It also accepts
  146. * more complex options such as IN, LIKE, or BETWEEN. If NULL, defaults to
  147. * the = operator.
  148. *
  149. * @return $this
  150. */
  151. public function addCondition($definition_field, $value, $operator = NULL) {
  152. if (!isset($operator)) {
  153. $this->conditions[$definition_field] = $value;
  154. }
  155. else {
  156. $this->conditions[$definition_field] = [$value, $operator];
  157. }
  158. return $this;
  159. }
  160. /**
  161. * Excludes links that are not enabled.
  162. *
  163. * @return $this
  164. */
  165. public function onlyEnabledLinks() {
  166. $this->addCondition('enabled', 1);
  167. return $this;
  168. }
  169. /**
  170. * Ensures only the top level of the tree is loaded.
  171. *
  172. * @return $this
  173. */
  174. public function setTopLevelOnly() {
  175. $this->setMaxDepth(1);
  176. return $this;
  177. }
  178. /**
  179. * Excludes the root menu link from the tree.
  180. *
  181. * Note that this is only necessary when you specified a custom root, because
  182. * the normal root ID is the empty string, '', which does not correspond to an
  183. * actual menu link. Hence when loading a menu link tree without specifying a
  184. * custom root the tree will start at the children even if this method has not
  185. * been called.
  186. *
  187. * @return $this
  188. */
  189. public function excludeRoot() {
  190. $this->setMinDepth(1);
  191. return $this;
  192. }
  193. /**
  194. * {@inheritdoc}
  195. */
  196. public function serialize() {
  197. return serialize($this->__serialize());
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function unserialize($serialized) {
  203. $this->__unserialize(unserialize($serialized));
  204. return $this;
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function __serialize(): array {
  210. // Enforce type consistency for all the internal properties of this object.
  211. $this->root = (string) $this->root;
  212. $this->minDepth = $this->minDepth !== NULL ? (int) $this->minDepth : NULL;
  213. $this->maxDepth = $this->maxDepth !== NULL ? (int) $this->maxDepth : NULL;
  214. $this->activeTrail = array_values(array_filter($this->activeTrail));
  215. // Sort 'expanded' and 'conditions' to prevent duplicate cache items.
  216. sort($this->expandedParents);
  217. asort($this->conditions);
  218. return [
  219. 'root' => $this->root,
  220. 'minDepth' => $this->minDepth,
  221. 'maxDepth' => $this->maxDepth,
  222. 'expandedParents' => $this->expandedParents,
  223. 'activeTrail' => $this->activeTrail,
  224. 'conditions' => $this->conditions,
  225. ];
  226. }
  227. /**
  228. * {@inheritdoc}
  229. */
  230. public function __unserialize(array $data): void {
  231. foreach ($data as $key => $value) {
  232. $this->{$key} = $value;
  233. }
  234. }
  235. }