3 namespace Drupal\bootstrap\Plugin;
5 use Drupal\bootstrap\Bootstrap;
6 use Drupal\bootstrap\Theme;
7 use Drupal\Core\Plugin\DefaultPluginManager;
10 * Base class for Bootstrap plugin managers.
14 class PluginManager extends DefaultPluginManager {
19 * @var \Drupal\bootstrap\Theme
24 * The theme handler to check if theme exists.
26 * @var \Drupal\Core\Extension\ThemeHandlerInterface
28 protected $themeHandler;
31 * The theme manager to invoke alter hooks.
33 * @var \Drupal\Core\Theme\ThemeManager
35 protected $themeManager;
38 * Creates the discovery object.
40 * @param \Drupal\bootstrap\Theme $theme
41 * The theme to use for discovery.
42 * @param string|bool $subdir
43 * The plugin's subdirectory, for example Plugin/views/filter.
44 * @param string|null $plugin_interface
45 * (optional) The interface each plugin should implement.
46 * @param string $plugin_definition_annotation_name
47 * (optional) Name of the annotation that contains the plugin definition.
48 * Defaults to 'Drupal\Component\Annotation\Plugin'.
50 public function __construct(Theme $theme, $subdir, $plugin_interface = NULL, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') {
51 // Get the active theme.
52 $this->theme = $theme;
54 // Determine the namespaces to search for.
56 foreach ($theme->getAncestry() as $ancestor) {
57 $namespaces['Drupal\\' . $ancestor->getName()] = [DRUPAL_ROOT . '/' . $ancestor->getPath() . '/src'];
59 $this->namespaces = new \ArrayObject($namespaces);
61 $this->subdir = $subdir;
62 $this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name;
63 $this->pluginInterface = $plugin_interface;
64 $this->themeHandler = \Drupal::service('theme_handler');
65 $this->themeManager = \Drupal::service('theme.manager');
71 protected function alterDefinitions(&$definitions) {
72 if ($this->alterHook) {
73 $this->themeManager->alter($this->alterHook, $definitions);
80 public function createInstance($plugin_id, array $configuration = []) {
81 if (!isset($configuration['theme'])) {
82 $configuration['theme'] = $this->theme;
84 return parent::createInstance($plugin_id, $configuration);
88 * Retrieves the cache tags used to invalidate caches.
91 * An indexed array of cache tags.
93 public function getCacheTags() {
94 return [Bootstrap::CACHE_TAG];
100 public function getDefinitions($sorted = TRUE) {
101 $definitions = parent::getDefinitions();
103 uasort($definitions, ['\Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
109 * Retrieves all definitions where the plugin ID matches a certain criteria.
111 * @param string $regex
112 * The regex pattern to match.
115 * An array of plugin definitions (empty array if no definitions were
116 * found). Keys are plugin IDs.
118 public function getDefinitionsLike($regex) {
120 foreach ($this->getDefinitions() as $plugin_id => $definition) {
121 if (preg_match($regex, $plugin_id)) {
122 $definitions[$plugin_id] = $definition;
125 ksort($definitions, SORT_NATURAL);
132 protected function providerExists($provider) {
133 return $this->themeHandler->themeExists($provider);