3 namespace Drupal\bootstrap_layouts;
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Extension\ThemeHandlerInterface;
9 use Drupal\Core\Plugin\DefaultPluginManager;
10 use Drupal\Core\StringTranslation\StringTranslationTrait;
11 use Drupal\Core\Theme\ThemeManagerInterface;
12 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
13 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
16 class BootstrapLayoutsPluginManager extends DefaultPluginManager implements ContainerInjectionInterface, ContainerAwareInterface {
18 use ContainerAwareTrait;
19 use StringTranslationTrait;
22 * @var \Drupal\Core\Extension\ThemeHandlerInterface
24 protected $themeHandler;
27 * @var \Drupal\Core\Theme\ThemeManagerInterface
29 protected $themeManager;
32 * Base plugin manager for Bootstrap Layouts plugin managers.
34 * The "container.namespaces" service does not contain theme namespaces
35 * since themes are not registered in the container. To allow themes to be
36 * able to participate in these plugins, the normal "namespaces" provided
37 * must be appending with the missing autoloader prefixes of the themes.
39 * @param \Traversable $namespaces
40 * An object that implements \Traversable which contains the root paths
41 * keyed by the corresponding namespace to look for plugin implementations.
42 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
43 * Cache backend instance to use.
44 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
45 * The module handler to invoke the alter hook with.
46 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
47 * The theme manager used to invoke the alter hook with.
48 * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
49 * The theme manager used to invoke the alter hook with.
50 * @param string|null $plugin_interface
51 * (optional) The interface each plugin should implement.
52 * @param string $plugin_definition_annotation_name
53 * (optional) The name of the annotation that contains the plugin definition.
54 * Defaults to 'Drupal\Component\Annotation\Plugin'.
56 public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, ThemeManagerInterface $theme_manager, $plugin_interface = NULL, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') {
57 /** @var \Composer\Autoload\ClassLoader $class_loader */
58 $class_loader = \Drupal::service('class_loader');
60 /** @var \ArrayObject $namespaces */
61 $ns = $namespaces->getArrayCopy();
63 foreach ($class_loader->getPrefixesPsr4() as $prefix => $paths) {
64 // Remove trailing path separators.
65 $prefix = trim($prefix, '\\');
67 // Remove the DRUPAL_ROOT prefix.
68 $path = str_replace(\Drupal::root() . '/', '', reset($paths));
70 // Only add missing contrib theme namespaces.
71 if (preg_match('/^(core|vendor)/', $path) === 0 && !isset($namespaces[$prefix])) {
76 // Replace the namespaces data.
77 $namespaces->exchangeArray($ns);
79 // Construct the plugin manager now.
80 parent::__construct('Plugin/BootstrapLayouts', $namespaces, $module_handler, $plugin_interface, $plugin_definition_annotation_name);
82 // Set the theme handler and manager.
83 $this->themeHandler = $theme_handler;
84 $this->themeManager = $theme_manager;
90 public static function create(ContainerInterface $container) {
92 $container->get('container.namespaces'),
93 $container->get('cache.discovery'),
94 $container->get('module_handler'),
95 $container->get('theme_handler'),
96 $container->get('theme.manager')
103 protected function alterDefinitions(&$definitions) {
104 if ($this->alterHook) {
105 $this->moduleHandler->alter($this->alterHook, $definitions);
106 $this->themeManager->alter($this->alterHook, $definitions);
113 protected function providerExists($provider) {
114 return $this->moduleHandler->moduleExists($provider) || $this->themeHandler->themeExists($provider);