getActiveLink example


  public function getBlockContents() {
    // We hard-code the menu name here since otherwise a link in the tools menu     // or elsewhere could give us a blank block.     $link = $this->menuActiveTrail->getActiveLink('admin');
    if ($link && $content = $this->getAdminBlock($link)) {
      $output = [
        '#theme' => 'admin_block_content',
        '#content' => $content,
      ];
    }
    else {
      $output = [
        '#markup' => t('You do not have any administrative items.'),
      ];
    }
    

  public function testGetActiveLink(Request $request$links$menu_name$expected_link) {
    $this->requestStack->push($request);
    if ($links !== FALSE) {
      $this->menuLinkManager->expects($this->exactly(2))
        ->method('loadLinksbyRoute')
        ->with('baby_llama')
        ->willReturn($links);
    }
    // Test with menu name.     $this->assertSame($expected_link$this->menuActiveTrail->getActiveLink($menu_name));
    // Test without menu name.     $this->assertSame($expected_link$this->menuActiveTrail->getActiveLink());
  }

  /** * Tests getActiveTrailIds(). * * @covers ::getActiveTrailIds * @dataProvider provider */
  public function testGetActiveTrailIds(Request $request$links$menu_name$expected_link$expected_trail) {
    
/** * Helper method for ::getActiveTrailIds(). */
  protected function doGetActiveTrailIds($menu_name) {
    // Parent ids; used both as key and value to ensure uniqueness.     // We always want all the top-level links with parent == ''.     $active_trail = ['' => ''];

    // If a link in the given menu indeed matches the route, then use it to     // complete the active trail.     if ($active_link = $this->getActiveLink($menu_name)) {
      if ($parents = $this->menuLinkManager->getParentIds($active_link->getPluginId())) {
        $active_trail = $parents + $active_trail;
      }
    }

    return $active_trail;
  }

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