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