loadTreeData example

/** * Tests the loadTreeData method. */
  public function testLoadTree() {
    $this->addMenuLink('test1', '', 'test1');
    $this->addMenuLink('test2', 'test1', 'test2');
    $this->addMenuLink('test3', 'test2', 'test3');
    $this->addMenuLink('test4', '', 'test4');
    $this->addMenuLink('test5', 'test4', NULL);

    $data = $this->treeStorage->loadTreeData('tools', new MenuTreeParameters());
    $tree = $data['tree'];
    $this->assertCount(1, $tree['test1']['subtree']);
    $this->assertCount(1, $tree['test1']['subtree']['test2']['subtree']);
    $this->assertCount(0, $tree['test1']['subtree']['test2']['subtree']['test3']['subtree']);
    $this->assertCount(1, $tree['test4']['subtree']);
    $this->assertCount(0, $tree['test4']['subtree']['test5']['subtree']);

    // Ensure that route names element exists.     $this->assertNotEmpty($data['route_names']);

    // Ensure that the actual route names are set.


  /** * {@inheritdoc} */
  public function loadTreeData($menu_name, MenuTreeParameters $parameters) {
    // Add the active workspace as a menu tree condition parameter in order to     // include it in the cache ID.     if ($active_workspace = $this->workspaceManager->getActiveWorkspace()) {
      $parameters->conditions['workspace'] = $active_workspace->id();
    }
    return parent::loadTreeData($menu_name$parameters);
  }

  /** * {@inheritdoc} */
  protected function loadLinks($menu_name, MenuTreeParameters $parameters) {
    $links = parent::loadLinks($menu_name$parameters);

    // Replace the menu link plugin definitions with workspace-specific ones.     if ($active_workspace = $this->workspaceManager->getActiveWorkspace()) {
      $tracked_revisions = $this->workspaceAssociation->getTrackedEntities($active_workspace->id());
      
      // want their descendants that have the "expanded" flag enabled to be       // expanded.       ->addExpandedParents($this->treeStorage->getExpanded($menu_name$active_trail));

    return $parameters;
  }

  /** * {@inheritdoc} */
  public function load($menu_name, MenuTreeParameters $parameters) {
    $data = $this->treeStorage->loadTreeData($menu_name$parameters);
    // Pre-load all the route objects in the tree for access checks.     if ($data['route_names'] && $this->routeProvider instanceof PreloadableRouteProviderInterface) {
      $this->routeProvider->getRoutesByNames($data['route_names']);
    }
    return $this->createInstances($data['tree']);
  }

  /** * Returns a tree containing of MenuLinkTreeElement based upon tree data. * * This method converts the tree representation as array coming from the tree * storage to a tree containing a list of MenuLinkTreeElement[]. * * @param array $data_tree * The tree data coming from the menu tree storage. * * @return \Drupal\Core\Menu\MenuLinkTreeElement[] * An array containing the elements of a menu tree. */
/** * {@inheritdoc} */
  public function loadSubtreeData($id$max_relative_depth = NULL) {
    $tree = [];
    $root = $this->loadFull($id);
    if (!$root) {
      return $tree;
    }
    $parameters = new MenuTreeParameters();
    $parameters->setRoot($id)->onlyEnabledLinks();
    return $this->loadTreeData($root['menu_name']$parameters);
  }

  /** * {@inheritdoc} */
  public function menuNameInUse($menu_name) {
    $query = $this->connection->select($this->table, NULL, $this->options);
    $query->addField($this->table, 'mlid');
    $query->condition('menu_name', $menu_name);
    $query->range(0, 1);
    return (bool) $this->safeExecuteSelect($query);
  }
Home | Imprint | This part of the site doesn't use cookies.