diff --git a/core/core.services.yml b/core/core.services.yml index da50e1a..b0bc12e 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -378,3 +378,4 @@ services: factory_service: image.toolkit.manager breadcrumb: class: Drupal\Core\Breadcrumb\BreadcrumbManager + arguments: ['%container.namespaces%'] diff --git a/core/lib/Drupal/Core/Breadcrumb/BreadcrumbManager.php b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbManager.php index d8b1dbe..38c1970 100644 --- a/core/lib/Drupal/Core/Breadcrumb/BreadcrumbManager.php +++ b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbManager.php @@ -9,21 +9,28 @@ use Symfony\Component\HttpFoundation\Request; +use Drupal\Component\Plugin\PluginManagerInterface; +use Drupal\Component\Plugin\PluginManagerGeneric; +use Drupal\Component\Plugin\Factory\DefaultFactory; + +use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; +use Drupal\Core\Plugin\Discovery\CacheDecorator; + + /** * Breadcrumb manager. * - * Holds an array of path processor objects and uses them to sequentially process - * a path, in order of processor priority. + * Holds an array of path processor objects and uses them to sequentially + * process a path, in order of processor priority. */ class BreadcrumbManager implements BreadcrumbBuilderInterface { /** - * Holds the array of breadcrumb builders, sorted by priority. + * Holds the plugin manager that knows all the breadcrumb plugins. * - * @var array - * An array of breadcrumb builders sorted by priorities. + * @var PluginManagerInterface */ - protected $builders = array(); + protected $pluginManager; /** * Holds the array of breadcrumb builders sorted by priority. @@ -34,19 +41,19 @@ class BreadcrumbManager implements BreadcrumbBuilderInterface { */ protected $sortedBuilders; - /* - * Add another breadcrumb builder. + function addBuilder() {} + + /** + * Constructs a BreadcrumbManager object. * - * @param \Drupal\Core\Breadcrumb\BreadcrumbBuilder $builder - * The breadcrumb builder to add. - * @param int $priority - * Priority of the breadcrumb builder. + * @param array $namespaces + * An array of paths keyed by it's corresponding namespaces. */ - public function addBuilder(BreadcrumbBuilderInterface $builder, $priority) { - $this->builders[$priority][] = $builder; - // The internal $this->builders array always needs to be correctly sorted. - // Builders with high priority are first in the array. - krsort($this->builders); + public function __construct(array $namespaces) { + $discovery = new AnnotatedClassDiscovery('Core', 'Breadcrumb', $namespaces); + // $discovery = new CacheDecorator($discovery, 'core.breadcrumb.builder'); + $factory = new DefaultFactory($discovery); + $this->pluginManager = new PluginManagerGeneric($discovery, $factory); } /** @@ -81,14 +88,33 @@ public function build(Request $request) { */ protected function getSortedBuilders() { if (!isset($this->sortedBuilders)) { - // Sort the builders according to priority. - krsort($this->builders); - // Merge the nested $this->builders array into $this->sortedBuilders. - $this->sortedBuilders = array(); - foreach ($this->builders as $builders) { - $this->sortedBuilders = array_merge($this->sortedBuilders, $builders); - } + $this->initSortedBuilders(); } return $this->sortedBuilders; } + + /** + * Initializes and fills the protected $this->sortedBuilders array. + */ + protected function initSortedBuilders() { + + // Get definitions from the plugin manager, and group them by weight. + $ids_by_weight = array(); + foreach ($this->pluginManager->getDefinitions() as $plugin_id => $definition) { + $weight = isset($definition['weight']) ? $definition['weight'] : 0; + $ids_by_weight[$weight][] = $plugin_id; + } + + // Sort the plugins according to priority. + krsort($ids_by_weight); + + // Merge the nested $ids_by_weight array, and instantiate the plugins. + $this->sortedBuilders = array(); + foreach ($ids_by_weight as $ids) { + foreach ($ids as $plugin_id) { + $plugin = $this->pluginManager->createInstance($plugin_id); + $this->sortedBuilders[] = $plugin; + } + } + } } diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index 58a0f75..8734772 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -16,7 +16,6 @@ use Drupal\Core\DependencyInjection\Compiler\RegisterRouteEnhancersPass; use Drupal\Core\DependencyInjection\Compiler\RegisterParamConvertersPass; use Drupal\Core\DependencyInjection\Compiler\RegisterServicesForDestructionPass; -use Drupal\Core\DependencyInjection\Compiler\RegisterBreadcrumbBuilderPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Reference; @@ -63,9 +62,6 @@ public function build(ContainerBuilder $container) { // Add the compiler pass that will process the tagged services. $container->addCompilerPass(new RegisterPathProcessorsPass()); $container->addCompilerPass(new ListCacheBinsPass()); - // Add the compiler pass that will process the tagged breadcrumb builder - // services. - $container->addCompilerPass(new RegisterBreadcrumbBuilderPass()); } /** diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterBreadcrumbBuilderPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterBreadcrumbBuilderPass.php deleted file mode 100644 index c123b53..0000000 --- a/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterBreadcrumbBuilderPass.php +++ /dev/null @@ -1,30 +0,0 @@ -hasDefinition('breadcrumb')) { - return; - } - $manager = $container->getDefinition('breadcrumb'); - foreach ($container->findTaggedServiceIds('breadcrumb_builder') as $id => $attributes) { - $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0; - $manager->addMethodCall('addBuilder', array(new Reference($id), $priority)); - } - } -} diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkBundle.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkBundle.php deleted file mode 100644 index 0cc9d0d..0000000 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkBundle.php +++ /dev/null @@ -1,30 +0,0 @@ -register('menu_link_breadcrumb', 'Drupal\menu_link\MenuLinkBreadcrumbBuilder') - ->addArgument(new Reference('request')) - ->addTag('breadcrumb_builder', array('priority' => 200)); - } -} diff --git a/core/modules/menu_link/menu_link.services.yml b/core/modules/menu_link/menu_link.services.yml deleted file mode 100644 index a428476..0000000 --- a/core/modules/menu_link/menu_link.services.yml +++ /dev/null @@ -1,5 +0,0 @@ -services: - menu_link.breadcrumb: - class: Drupal\menu_link\MenuLinkBreadcrumbBuilder - tags: - - { name: breadcrumb_builder, priority: 0 } diff --git a/core/modules/system/system.services.yml b/core/modules/system/system.services.yml index 7f36e61..bf43cd4 100644 --- a/core/modules/system/system.services.yml +++ b/core/modules/system/system.services.yml @@ -6,7 +6,3 @@ services: plugin.manager.system.plugin_ui: class: Drupal\system\Plugin\Type\PluginUIManager arguments: ['%container.namespaces%'] - system.breadcrumb.legacy: - class: Drupal\system\LegacyBreadcrumbBuilder - tags: - - {name: breadcrumb_builder, priority: 500}