3 namespace Drupal\Core\Breadcrumb;
5 use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
6 use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
8 use Drupal\Core\Render\RenderableInterface;
11 * Used to return generated breadcrumbs with associated cacheability metadata.
13 class Breadcrumb implements RenderableInterface, RefinableCacheableDependencyInterface {
15 use RefinableCacheableDependencyTrait;
18 * An ordered list of links for the breadcrumb.
20 * @var \Drupal\Core\Link[]
22 protected $links = [];
25 * Gets the breadcrumb links.
27 * @return \Drupal\Core\Link[]
29 public function getLinks() {
34 * Sets the breadcrumb links.
36 * @param \Drupal\Core\Link[] $links
37 * The breadcrumb links.
41 * @throws \LogicException
42 * Thrown when setting breadcrumb links after they've already been set.
44 public function setLinks(array $links) {
45 if (!empty($this->links)) {
46 throw new \LogicException('Once breadcrumb links are set, only additional breadcrumb links can be added.');
49 $this->links = $links;
55 * Appends a link to the end of the ordered list of breadcrumb links.
57 * @param \Drupal\Core\Link $link
58 * The link appended to the breadcrumb.
62 public function addLink(Link $link) {
63 $this->links[] = $link;
71 public function toRenderable() {
74 'contexts' => $this->cacheContexts,
75 'tags' => $this->cacheTags,
76 'max-age' => $this->cacheMaxAge,
79 if (!empty($this->links)) {
81 '#theme' => 'breadcrumb',
82 '#links' => $this->links,