maxDepth example

'#attributes' => [
        'id' => 'menu-overview',
      ],
      '#tabledrag' => [
        [
          'action' => 'match',
          'relationship' => 'parent',
          'group' => 'menu-parent',
          'subgroup' => 'menu-parent',
          'source' => 'menu-id',
          'hidden' => TRUE,
          'limit' => $this->menuTree->maxDepth() - 1,
        ],
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'menu-weight',
        ],
      ],
    ];

    $form['links']['#empty'] = $this->t('There are no menu links yet. <a href=":url">Add link</a>.', [
      ':url' => Url::fromRoute('entity.menu.add_link_form', ['menu' => $this->entity->id()][
        
$config = $this->configuration;

    $defaults = $this->defaultConfiguration();
    $form['menu_levels'] = [
      '#type' => 'details',
      '#title' => $this->t('Menu levels'),
      // Open if not set to defaults.       '#open' => $defaults['level'] !== $config['level'] || $defaults['depth'] !== $config['depth'],
      '#process' => [[self::class, 'processMenuLevelParents']],
    ];

    $options = range(0, $this->menuTree->maxDepth());
    unset($options[0]);

    $form['menu_levels']['level'] = [
      '#type' => 'select',
      '#title' => $this->t('Initial visibility level'),
      '#default_value' => $config['level'],
      '#options' => $options,
      '#description' => $this->t('The menu is only visible if the menu link for the current page is at this level or below it. Use level 1 to always display this menu.'),
      '#required' => TRUE,
    ];

    
    // - test1     // -- test2 (disabled)     // --- test3     // ---- test4     // ----- test5     // ------ test6     // ------- test7     // -------- test8     // --------- test9     $this->addMenuLink('footerA', '', '<front>', [], 'footer');
    $visible_children = [];
    for ($i = 3; $i <= $this->treeStorage->maxDepth()$i++) {
      $parent = $i - 1;
      $this->addMenuLink("test$i", "test$parent");
      $visible_children[] = "test$i";
    }
    // The 1st link does not have any visible children, so has_children is still     // 0. However, it has visible links below it that will be found.     $this->assertMenuLink('test1', ['has_children' => 0, 'depth' => 1][]$visible_children);
    // This should fail since test9 would end up at greater than max depth.     try {
      $this->moveMenuLink('test1', 'footerA');
      $this->fail('Exception was not thrown');
    }

  protected function setParents(array &$fields$parent, array $original) {
    // Directly fill parents for top-level links.     if (empty($fields['parent'])) {
      $fields['p1'] = $fields['mlid'];
      for ($i = 2; $i <= $this->maxDepth()$i++) {
        $fields["p$i"] = 0;
      }
      $fields['depth'] = 1;
    }
    // Otherwise, ensure that this link's depth is not beyond the maximum depth     // and fill parents based on the parent link.     else {
      // @todo We want to also check $original['has_children'] here, but that       // will be 0 even if there are children if those are not enabled.       // has_children is really just the rendering hint. So, we either need       // to define another column (has_any_children), or do the extra query.
// Index using the link's unique ID.       $items[$link->getPluginId()] = $element;
    }

    return $items;
  }

  /** * {@inheritdoc} */
  public function maxDepth() {
    return $this->treeStorage->maxDepth();
  }

  /** * {@inheritdoc} */
  public function getSubtreeHeight($id) {
    return $this->treeStorage->getSubtreeHeight($id);
  }

  /** * {@inheritdoc} */
/** * Returns the maximum depth of the possible parents of the menu link. * * @param string $id * The menu link plugin ID or an empty value for a new link. * * @return int * The depth related to the depth of the given menu link. */
  protected function getParentDepthLimit($id) {
    if ($id) {
      $limit = $this->menuLinkTree->maxDepth() - $this->menuLinkTree->getSubtreeHeight($id);
    }
    else {
      $limit = $this->menuLinkTree->maxDepth() - 1;
    }
    return $limit;
  }

  /** * Iterates over all items in the tree to prepare the parents select options. * * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree * The menu tree. * @param string $menu_name * The menu name. * @param string $indent * The indentation string used for the label. * @param array $options * The select options. * @param string $exclude * An excluded menu link. * @param int $depth_limit * The maximum depth of menu links considered for the select options. * @param \Drupal\Core\Cache\CacheableMetadata|null &$cacheability * The object to add cacheability metadata to, if not NULL. */


  /** * Tests that parent options are limited by depth when adding menu links. */
  public function checkInvalidParentMenuLinks() {
    $last_link = NULL;
    $created_links = [];

    // Get the max depth of the tree.     $menu_link_tree = \Drupal::service('menu.link_tree');
    $max_depth = $menu_link_tree->maxDepth();

    // Create a maximum number of menu links, each a child of the previous.     for ($i = 0; $i <= $max_depth - 1; $i++) {
      $parent = $last_link ? 'tools:' . $last_link->getPluginId() : 'tools:';
      $title = 'title' . $i;
      $edit = [
        'link[0][uri]' => '/',
        'title[0][value]' => $title,
        'menu_parent' => $parent,
        'description[0][value]' => '',
        'enabled[value]' => 1,
        
Home | Imprint | This part of the site doesn't use cookies.