class BreadcrumbManager
Same name in other branches
- 9 core/lib/Drupal/Core/Breadcrumb/BreadcrumbManager.php \Drupal\Core\Breadcrumb\BreadcrumbManager
- 10 core/lib/Drupal/Core/Breadcrumb/BreadcrumbManager.php \Drupal\Core\Breadcrumb\BreadcrumbManager
- 11.x core/lib/Drupal/Core/Breadcrumb/BreadcrumbManager.php \Drupal\Core\Breadcrumb\BreadcrumbManager
Provides a breadcrumb manager.
Can be assigned any number of BreadcrumbBuilderInterface objects by calling the addBuilder() method. When build() is called it iterates over the objects in priority order and uses the first one that returns TRUE from BreadcrumbBuilderInterface::applies() to build the breadcrumbs.
Hierarchy
- class \Drupal\Core\Breadcrumb\BreadcrumbManager implements \Drupal\Core\Breadcrumb\ChainBreadcrumbBuilderInterface
Expanded class hierarchy of BreadcrumbManager
See also
\Drupal\Core\DependencyInjection\Compiler\RegisterBreadcrumbBuilderPass
1 file declares its use of BreadcrumbManager
- BreadcrumbManagerTest.php in core/
tests/ Drupal/ Tests/ Core/ Breadcrumb/ BreadcrumbManagerTest.php
1 string reference to 'BreadcrumbManager'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses BreadcrumbManager
File
-
core/
lib/ Drupal/ Core/ Breadcrumb/ BreadcrumbManager.php, line 18
Namespace
Drupal\Core\BreadcrumbView source
class BreadcrumbManager implements ChainBreadcrumbBuilderInterface {
/**
* The module handler to invoke the alter hook.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Holds arrays of breadcrumb builders, keyed by priority.
*
* @var array
*/
protected $builders = [];
/**
* Holds the array of breadcrumb builders sorted by priority.
*
* Set to NULL if the array needs to be re-calculated.
*
* @var \Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface[]|null
*/
protected $sortedBuilders;
/**
* Constructs a \Drupal\Core\Breadcrumb\BreadcrumbManager object.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(ModuleHandlerInterface $module_handler) {
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public function addBuilder(BreadcrumbBuilderInterface $builder, $priority) {
$this->builders[$priority][] = $builder;
// Force the builders to be re-sorted.
$this->sortedBuilders = NULL;
}
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$context = [
'builder' => NULL,
];
// Call the build method of registered breadcrumb builders,
// until one of them returns an array.
foreach ($this->getSortedBuilders() as $builder) {
if (!$builder->applies($route_match)) {
// The builder does not apply, so we continue with the other builders.
continue;
}
$breadcrumb = $builder->build($route_match);
if ($breadcrumb instanceof Breadcrumb) {
$context['builder'] = $builder;
break;
}
else {
throw new \UnexpectedValueException('Invalid breadcrumb returned by ' . get_class($builder) . '::build().');
}
}
// Allow modules to alter the breadcrumb.
$this->moduleHandler
->alter('system_breadcrumb', $breadcrumb, $route_match, $context);
return $breadcrumb;
}
/**
* Returns the sorted array of breadcrumb builders.
*
* @return \Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface[]
* An array of breadcrumb builder objects.
*/
protected function getSortedBuilders() {
if (!isset($this->sortedBuilders)) {
// Sort the builders according to priority.
krsort($this->builders);
// Merge nested builders from $this->builders into $this->sortedBuilders.
$this->sortedBuilders = [];
foreach ($this->builders as $builders) {
$this->sortedBuilders = array_merge($this->sortedBuilders, $builders);
}
}
return $this->sortedBuilders;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
BreadcrumbManager::$builders | protected | property | Holds arrays of breadcrumb builders, keyed by priority. | |
BreadcrumbManager::$moduleHandler | protected | property | The module handler to invoke the alter hook. | |
BreadcrumbManager::$sortedBuilders | protected | property | Holds the array of breadcrumb builders sorted by priority. | |
BreadcrumbManager::addBuilder | public | function | Adds another breadcrumb builder. | Overrides ChainBreadcrumbBuilderInterface::addBuilder |
BreadcrumbManager::applies | public | function | Whether this breadcrumb builder should be used to build the breadcrumb. | Overrides BreadcrumbBuilderInterface::applies |
BreadcrumbManager::build | public | function | Builds the breadcrumb. | Overrides BreadcrumbBuilderInterface::build |
BreadcrumbManager::getSortedBuilders | protected | function | Returns the sorted array of breadcrumb builders. | |
BreadcrumbManager::__construct | public | function | Constructs a \Drupal\Core\Breadcrumb\BreadcrumbManager object. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.