Chris@0: root = (string) $root; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets a minimum depth for menu tree loading. Chris@0: * Chris@0: * @param int $min_depth Chris@0: * The (root-relative) minimum depth to apply. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setMinDepth($min_depth) { Chris@0: $this->minDepth = max(1, $min_depth); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets a maximum depth for menu tree loading. Chris@0: * Chris@0: * @param int $max_depth Chris@0: * The (root-relative) maximum depth to apply. Chris@0: * Chris@0: * @return $this Chris@0: * Chris@0: * @codeCoverageIgnore Chris@0: */ Chris@0: public function setMaxDepth($max_depth) { Chris@0: $this->maxDepth = $max_depth; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds parent menu links IDs to restrict the tree. Chris@0: * Chris@0: * @param string[] $parents Chris@0: * An array containing parent IDs. If supplied, the tree is limited to Chris@0: * links that have these parents. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function addExpandedParents(array $parents) { Chris@0: $this->expandedParents = array_merge($this->expandedParents, $parents); Chris@0: $this->expandedParents = array_unique($this->expandedParents); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the active trail IDs used to set the inActiveTrail property. Chris@0: * Chris@0: * @param string[] $active_trail Chris@0: * An array containing the active trail: a list of menu link plugin IDs. Chris@0: * Chris@0: * @return $this Chris@0: * Chris@0: * @see \Drupal\Core\Menu\MenuActiveTrail::getActiveTrailIds() Chris@0: * Chris@0: * @codeCoverageIgnore Chris@0: */ Chris@0: public function setActiveTrail(array $active_trail) { Chris@0: $this->activeTrail = $active_trail; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a custom query condition. Chris@0: * Chris@0: * @param string $definition_field Chris@0: * Only conditions that are testing menu link definition fields are allowed. Chris@0: * @param mixed $value Chris@0: * The value to test the link definition field against. In most cases, this Chris@0: * is a scalar. For more complex options, it is an array. The meaning of Chris@0: * each element in the array is dependent on the $operator. Chris@0: * @param string|null $operator Chris@0: * (optional) The comparison operator, such as =, <, or >=. It also accepts Chris@0: * more complex options such as IN, LIKE, or BETWEEN. If NULL, defaults to Chris@0: * the = operator. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function addCondition($definition_field, $value, $operator = NULL) { Chris@0: if (!isset($operator)) { Chris@0: $this->conditions[$definition_field] = $value; Chris@0: } Chris@0: else { Chris@0: $this->conditions[$definition_field] = [$value, $operator]; Chris@0: } Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Excludes links that are not enabled. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function onlyEnabledLinks() { Chris@0: $this->addCondition('enabled', 1); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensures only the top level of the tree is loaded. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setTopLevelOnly() { Chris@0: $this->setMaxDepth(1); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Excludes the root menu link from the tree. Chris@0: * Chris@0: * Note that this is only necessary when you specified a custom root, because Chris@0: * the normal root ID is the empty string, '', which does not correspond to an Chris@0: * actual menu link. Hence when loading a menu link tree without specifying a Chris@0: * custom root the tree will start at the children even if this method has not Chris@0: * been called. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function excludeRoot() { Chris@0: $this->setMinDepth(1); Chris@0: return $this; Chris@0: } Chris@0: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function serialize() { Chris@18: // Enforce type consistency for all the internal properties of this object. Chris@18: $this->root = (string) $this->root; Chris@18: $this->minDepth = $this->minDepth !== NULL ? (int) $this->minDepth : NULL; Chris@18: $this->maxDepth = $this->maxDepth !== NULL ? (int) $this->maxDepth : NULL; Chris@18: $this->activeTrail = array_values(array_filter($this->activeTrail)); Chris@18: Chris@18: // Sort 'expanded' and 'conditions' to prevent duplicate cache items. Chris@18: sort($this->expandedParents); Chris@18: asort($this->conditions); Chris@18: Chris@18: return serialize([ Chris@18: 'root' => $this->root, Chris@18: 'minDepth' => $this->minDepth, Chris@18: 'maxDepth' => $this->maxDepth, Chris@18: 'expandedParents' => $this->expandedParents, Chris@18: 'activeTrail' => $this->activeTrail, Chris@18: 'conditions' => $this->conditions, Chris@18: ]); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function unserialize($serialized) { Chris@18: foreach (unserialize($serialized) as $key => $value) { Chris@18: $this->{$key} = $value; Chris@18: } Chris@18: Chris@18: return $this; Chris@18: } Chris@18: Chris@0: }