Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Breadcrumb / BreadcrumbManager.php
1 <?php
2
3 namespace Drupal\Core\Breadcrumb;
4
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\Routing\RouteMatchInterface;
7
8 /**
9  * Provides a breadcrumb manager.
10  *
11  * Can be assigned any number of BreadcrumbBuilderInterface objects by calling
12  * the addBuilder() method. When build() is called it iterates over the objects
13  * in priority order and uses the first one that returns TRUE from
14  * BreadcrumbBuilderInterface::applies() to build the breadcrumbs.
15  *
16  * @see \Drupal\Core\DependencyInjection\Compiler\RegisterBreadcrumbBuilderPass
17  */
18 class BreadcrumbManager implements ChainBreadcrumbBuilderInterface {
19
20   /**
21    * The module handler to invoke the alter hook.
22    *
23    * @var \Drupal\Core\Extension\ModuleHandlerInterface
24    */
25   protected $moduleHandler;
26
27   /**
28    * Holds arrays of breadcrumb builders, keyed by priority.
29    *
30    * @var array
31    */
32   protected $builders = [];
33
34   /**
35    * Holds the array of breadcrumb builders sorted by priority.
36    *
37    * Set to NULL if the array needs to be re-calculated.
38    *
39    * @var \Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface[]|null
40    */
41   protected $sortedBuilders;
42
43   /**
44    * Constructs a \Drupal\Core\Breadcrumb\BreadcrumbManager object.
45    *
46    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
47    *   The module handler.
48    */
49   public function __construct(ModuleHandlerInterface $module_handler) {
50     $this->moduleHandler = $module_handler;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function addBuilder(BreadcrumbBuilderInterface $builder, $priority) {
57     $this->builders[$priority][] = $builder;
58     // Force the builders to be re-sorted.
59     $this->sortedBuilders = NULL;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function applies(RouteMatchInterface $route_match) {
66     return TRUE;
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function build(RouteMatchInterface $route_match) {
73     $breadcrumb = new Breadcrumb();
74     $context = ['builder' => NULL];
75     // Call the build method of registered breadcrumb builders,
76     // until one of them returns an array.
77     foreach ($this->getSortedBuilders() as $builder) {
78       if (!$builder->applies($route_match)) {
79         // The builder does not apply, so we continue with the other builders.
80         continue;
81       }
82
83       $breadcrumb = $builder->build($route_match);
84
85       if ($breadcrumb instanceof Breadcrumb) {
86         $context['builder'] = $builder;
87         break;
88       }
89       else {
90         throw new \UnexpectedValueException('Invalid breadcrumb returned by ' . get_class($builder) . '::build().');
91       }
92     }
93     // Allow modules to alter the breadcrumb.
94     $this->moduleHandler->alter('system_breadcrumb', $breadcrumb, $route_match, $context);
95
96     return $breadcrumb;
97   }
98
99   /**
100    * Returns the sorted array of breadcrumb builders.
101    *
102    * @return \Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface[]
103    *   An array of breadcrumb builder objects.
104    */
105   protected function getSortedBuilders() {
106     if (!isset($this->sortedBuilders)) {
107       // Sort the builders according to priority.
108       krsort($this->builders);
109       // Merge nested builders from $this->builders into $this->sortedBuilders.
110       $this->sortedBuilders = [];
111       foreach ($this->builders as $builders) {
112         $this->sortedBuilders = array_merge($this->sortedBuilders, $builders);
113       }
114     }
115     return $this->sortedBuilders;
116   }
117
118 }