"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/lib/Drupal/Core/Menu/MenuTreeParameters.php" (26 Nov 2020, 6669 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "MenuTreeParameters.php" see the Fossies "Dox" file reference documentation.

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