createFromRoute example


  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = parent::build($route_match);
    $breadcrumb->addCacheContexts(['route']);

    $parents = $this->termStorage->loadAllParents($route_match->getParameter('node')->forum_tid);
    if ($parents) {
      $parents = array_reverse($parents);
      foreach ($parents as $parent) {
        $breadcrumb->addCacheableDependency($parent);
        $breadcrumb->addLink(Link::createFromRoute($parent->label(), 'forum.page',
          [
            'taxonomy_term' => $parent->id(),
          ]
        ));
      }
    }

    return $breadcrumb;
  }

}
// Add all parent forums to breadcrumbs.     /** @var \Drupal\taxonomy\TermInterface $term */
    $term = $route_match->getParameter('taxonomy_term');
    $term_id = $term->id();
    $breadcrumb->addCacheableDependency($term);

    $parents = $this->termStorage->loadAllParents($term_id);
    if ($parents) {
      foreach (array_reverse($parents) as $parent) {
        if ($parent->id() != $term_id) {
          $breadcrumb->addCacheableDependency($parent);
          $breadcrumb->addLink(Link::createFromRoute($parent->label(), 'forum.page', [
            'taxonomy_term' => $parent->id(),
          ]));
        }
      }
    }

    return $breadcrumb;
  }

}

function hook_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context) {
  // Add an item to the end of the breadcrumb.   $breadcrumb->addLink(\Drupal\Core\Link::createFromRoute(t('Text'), 'example_route_name'));
}

/** * @} End of "addtogroup hooks". */
->getMock();

    // Our data set.     $route_match = $this->createMock('Drupal\Core\Routing\RouteMatchInterface');
    $route_match->expects($this->exactly(2))
      ->method('getParameter')
      ->with('node')
      ->willReturn($forum_node);

    // First test.     $expected1 = [
      Link::createFromRoute('Home', '<front>'),
      Link::createFromRoute('Forums', 'forum.index'),
      Link::createFromRoute('Something', 'forum.page', ['taxonomy_term' => 1]),
    ];
    $breadcrumb = $breadcrumb_builder->build($route_match);
    $this->assertEquals($expected1$breadcrumb->getLinks());
    $this->assertEqualsCanonicalizing(['route']$breadcrumb->getCacheContexts());
    $this->assertEqualsCanonicalizing(['taxonomy_term:1', 'taxonomy_vocabulary:5']$breadcrumb->getCacheTags());
    $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());

    // Second test.     $expected2 = [
      
if ($bundle_entity_type_id) {
      $bundle_argument = $bundle_entity_type_id;
      $bundle_entity_type = $this->entityTypeManager->getDefinition($bundle_entity_type_id);
      $bundle_entity_type_label = $bundle_entity_type->getSingularLabel();
      $build['#cache']['tags'] = $bundle_entity_type->getListCacheTags();

      // Build the message shown when there are no bundles.       $link_text = $this->t('Add a new @entity_type.', ['@entity_type' => $bundle_entity_type_label]);
      $link_route_name = 'entity.' . $bundle_entity_type->id() . '.add_form';
      $build['#add_bundle_message'] = $this->t('There is no @entity_type yet. @add_link', [
        '@entity_type' => $bundle_entity_type_label,
        '@add_link' => Link::createFromRoute($link_text$link_route_name)->toString(),
      ]);
      // Filter out the bundles the user doesn't have access to.       $access_control_handler = $this->entityTypeManager->getAccessControlHandler($entity_type_id);
      foreach ($bundles as $bundle_name => $bundle_info) {
        $access = $access_control_handler->createAccess($bundle_name, NULL, [], TRUE);
        if (!$access->isAllowed()) {
          unset($bundles[$bundle_name]);
        }
        $this->renderer->addCacheableDependency($build$access);
      }
      // Add descriptions from the bundle entities.


    // Add a translation manager for t().     $translation_manager = $this->getStringTranslationStub();
    $breadcrumb_builder->setStringTranslation($translation_manager);

    // Our empty data set.     $route_match = $this->createMock('Drupal\Core\Routing\RouteMatchInterface');

    // Expected result set.     $expected = [
      Link::createFromRoute('Home', '<front>'),
      Link::createFromRoute('Fora_is_the_plural_of_forum', 'forum.index'),
    ];

    // And finally, the test.     $breadcrumb = $breadcrumb_builder->build($route_match);
    $this->assertEquals($expected$breadcrumb->getLinks());
    $this->assertEquals(['route']$breadcrumb->getCacheContexts());
    $this->assertEquals(['taxonomy_vocabulary:5']$breadcrumb->getCacheTags());
    $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
  }

}
$this->setStringTranslation($string_translation);
    $this->termStorage = $entity_type_manager->getStorage('taxonomy_term');
  }

  /** * {@inheritdoc} */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb->addCacheContexts(['route']);

    $links[] = Link::createFromRoute($this->t('Home'), '<front>');

    $vocabulary = $this->entityTypeManager
      ->getStorage('taxonomy_vocabulary')
      ->load($this->config->get('vocabulary'));
    $breadcrumb->addCacheableDependency($vocabulary);
    $links[] = Link::createFromRoute($vocabulary->label(), 'forum.index');

    return $breadcrumb->setLinks($links);
  }

}
public function toUrl(array $options = []) {
    return Url::fromRoute('help.help_topic', ['id' => $this->getPluginId()]$options);
  }

  /** * {@inheritdoc} */
  public function toLink($text = NULL, array $options = []) {
    if (!$text) {
      $text = $this->getLabel();
    }
    return Link::createFromRoute($text, 'help.help_topic', ['id' => $this->getPluginId()]$options);
  }

}
->expects($this->any())
      ->method('getDefinition')
      ->with($entity_type_id)
      ->willReturn($entity_type);

    /** @var \Drupal\Core\Entity\Entity $entity */
    $entity = $this->getMockForAbstractClass(ConfigEntityBase::class[
      ['id' => $entity_id, 'label' => $entity_label, 'langcode' => 'es'],
      $entity_type_id,
    ]);

    $expected_link = Link::createFromRoute(
      $expected_text,
      $route_name,
      [$entity_type_id => $entity_id],
      ['entity_type' => $entity_type_id, 'entity' => $entity] + $link_options
    );

    $result_link = $entity->toLink($link_text$link_rel$link_options);
    $this->assertEquals($expected_link$result_link);
  }

  /** * Provides test data for testLink(). */

  public function applies(RouteMatchInterface $route_match) {
    return $route_match->getRouteName() == 'comment.reply' && $route_match->getParameter('entity');
  }

  /** * {@inheritdoc} */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb->addCacheContexts(['route']);
    $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));

    $entity = $route_match->getParameter('entity');
    $breadcrumb->addLink(new Link($entity->label()$entity->toUrl()));
    $breadcrumb->addCacheableDependency($entity);

    if (($pid = $route_match->getParameter('pid')) && ($comment = $this->entityTypeManager->getStorage('comment')->load($pid))) {
      /** @var \Drupal\comment\CommentInterface $comment */
      $breadcrumb->addCacheableDependency($comment);
      // Display link to parent comment.       // @todo Clean-up permalink in https://www.drupal.org/node/2198041       $breadcrumb->addLink(new Link($comment->getSubject()$comment->toUrl()));
    }
    $this->verifyForumView($this->forumContainer);
    // View forum page.     $this->verifyForumView($this->forum, $this->forumContainer);
    // View root forum page.     $this->verifyForumView($this->rootForum);

    // View forum node.     $this->drupalGet('node/' . $node->id());
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->titleEquals($node->label() . ' | Drupal');
    $breadcrumb_build = [
      Link::createFromRoute('Home', '<front>'),
      Link::createFromRoute('Forums', 'forum.index'),
      Link::createFromRoute($this->forumContainer['name'], 'forum.page', ['taxonomy_term' => $this->forumContainer['tid']]),
      Link::createFromRoute($this->forum['name'], 'forum.page', ['taxonomy_term' => $this->forum['tid']]),
    ];
    $breadcrumb = [
      '#theme' => 'breadcrumb',
      '#links' => $breadcrumb_build,
    ];
    $this->assertSession()->responseContains(\Drupal::service('renderer')->renderRoot($breadcrumb));

    // View forum edit node.

  public function applies(RouteMatchInterface $route_match) {
    return $route_match->getRouteName() == 'entity.taxonomy_term.canonical'
      && $route_match->getParameter('taxonomy_term') instanceof TermInterface;
  }

  /** * {@inheritdoc} */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
    $term = $route_match->getParameter('taxonomy_term');
    // Breadcrumb needs to have terms cacheable metadata as a cacheable     // dependency even though it is not shown in the breadcrumb because e.g. its     // parent might have changed.     $breadcrumb->addCacheableDependency($term);
    // @todo This overrides any other possible breadcrumb and is a pure     // hard-coded presumption. Make this behavior configurable per     // vocabulary or term.     $parents = $this->entityTypeManager->getStorage('taxonomy_term')->loadAllParents($term->id());
    // Remove current term being accessed.     array_shift($parents);
    


  /** * {@inheritdoc} */
  public function listTopics() {
    $topics = [];
    $this->moduleHandler->invokeAllWith(
      'help',
      function Dcallable $hook, string $module) use (&$topics) {
        $title = $this->moduleHandler->getName($module);
        $topics[$title] = Link::createFromRoute($title, 'help.page', ['name' => $module]);
      }
    );

    // Sort topics by title, which is the array key above.     ksort($topics);
    return $topics;
  }

}
$node = $route_match->getParameter('node');
    return $node instanceof NodeInterface && !empty($node->book);
  }

  /** * {@inheritdoc} */
  public function build(RouteMatchInterface $route_match) {
    $book_nids = [];
    $breadcrumb = new Breadcrumb();

    $links = [Link::createFromRoute($this->t('Home'), '<front>', [][
      'language' => $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT),
    ]),
    ];
    $breadcrumb->addCacheContexts(['languages:' . LanguageInterface::TYPE_CONTENT]);
    $book = $route_match->getParameter('node')->book;
    $depth = 1;
    // We skip the current node.     while (!empty($book['p' . ($depth + 1)])) {
      $book_nids[] = $book['p' . $depth];
      $depth++;
    }
    
// Fallback to using the raw path component as the title if the             // route is missing a _title or _title_callback attribute.             $title = str_replace(['-', '_'], ' ', Unicode::ucfirst(end($path_elements)));
          }
          $url = Url::fromRouteMatch($route_match);
          $links[] = new Link($title$url);
        }
      }
    }

    // Add the Home link.     $links[] = Link::createFromRoute($this->t('Home'), '<front>');

    return $breadcrumb->setLinks(array_reverse($links));
  }

  /** * Matches a path in the router. * * @param string $path * The request path with a leading slash. * @param array $exclude * An array of paths or system paths to skip. * * @return \Symfony\Component\HttpFoundation\Request * A populated request object or NULL if the path couldn't be matched. */
Home | Imprint | This part of the site doesn't use cookies.