setActiveTrail example

/** * @covers ::serialize * @covers ::unserialize */
  public function testSerialize() {
    $parameters = new MenuTreeParameters();
    $parameters->setRoot(1);
    $parameters->setMinDepth('2');
    $parameters->setMaxDepth('9');
    $parameters->addExpandedParents(['', 'foo']);
    $parameters->setActiveTrail(['', 'bar']);

    $after_serialize = unserialize(serialize($parameters));
    $this->assertSame('1', $after_serialize->root);
    $this->assertSame(2, $after_serialize->minDepth);
    $this->assertSame(9, $after_serialize->maxDepth);
    $this->assertSame(['', 'foo']$after_serialize->expandedParents);
    $this->assertSame(['bar']$after_serialize->activeTrail);
  }

}
$this->configuration['expand_all_items'] = $form_state->getValue('expand_all_items');
  }

  /** * {@inheritdoc} */
  public function build() {
    $menu_name = $this->getDerivativeId();
    if ($this->configuration['expand_all_items']) {
      $parameters = new MenuTreeParameters();
      $active_trail = $this->menuActiveTrail->getActiveTrailIds($menu_name);
      $parameters->setActiveTrail($active_trail);
    }
    else {
      $parameters = $this->menuTree->getCurrentRouteMenuTreeParameters($menu_name);
    }

    // Adjust the menu tree parameters based on the block's configuration.     $level = $this->configuration['level'];
    $depth = $this->configuration['depth'];
    $parameters->setMinDepth($level);
    // When the depth is configured to zero, there is no depth limit. When depth     // is non-zero, it indicates the number of levels that must be displayed.
$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.     $this->assertContains('test1', $data['route_names']);
    $this->assertNotContains('test5', $data['route_names']);

    $parameters = new MenuTreeParameters();
    $parameters->setActiveTrail(['test4', 'test5']);
    $data = $this->treeStorage->loadTreeData('tools', $parameters);
    $tree = $data['tree'];
    $this->assertCount(1, $tree['test1']['subtree']);
    $this->assertFalse($tree['test1']['in_active_trail']);
    $this->assertCount(1, $tree['test1']['subtree']['test2']['subtree']);
    $this->assertFalse($tree['test1']['subtree']['test2']['in_active_trail']);
    $this->assertCount(0, $tree['test1']['subtree']['test2']['subtree']['test3']['subtree']);
    $this->assertFalse($tree['test1']['subtree']['test2']['subtree']['test3']['in_active_trail']);
    $this->assertCount(1, $tree['test4']['subtree']);
    $this->assertTrue($tree['test4']['in_active_trail']);
    $this->assertCount(0, $tree['test4']['subtree']['test5']['subtree']);
    
$this->menuActiveTrail = $menu_active_trail;
    $this->controllerResolver = $controller_resolver;
  }

  /** * {@inheritdoc} */
  public function getCurrentRouteMenuTreeParameters($menu_name) {
    $active_trail = $this->menuActiveTrail->getActiveTrailIds($menu_name);

    $parameters = new MenuTreeParameters();
    $parameters->setActiveTrail($active_trail)
      // We want links in the active trail to be expanded.       ->addExpandedParents($active_trail)
      // We marked the links in the active trail to be expanded, but we also       // want their descendants that have the "expanded" flag enabled to be       // expanded.       ->addExpandedParents($this->treeStorage->getExpanded($menu_name$active_trail));

    return $parameters;
  }

  /** * {@inheritdoc} */
Home | Imprint | This part of the site doesn't use cookies.