4 * Contains \Drupal\bootstrap\Plugin\PluginManager.
7 namespace Drupal\bootstrap\Plugin;
9 use Drupal\bootstrap\Bootstrap;
10 use Drupal\bootstrap\Theme;
11 use Drupal\Core\Plugin\DefaultPluginManager;
14 * Base class for Bootstrap plugin managers.
18 class PluginManager extends DefaultPluginManager {
23 * @var \Drupal\bootstrap\Theme
28 * The theme handler to check if theme exists.
30 * @var \Drupal\Core\Extension\ThemeHandlerInterface
32 protected $themeHandler;
35 * The theme manager to invoke alter hooks.
37 * @var \Drupal\Core\Theme\ThemeManager
39 protected $themeManager;
42 * Creates the discovery object.
44 * @param \Drupal\bootstrap\Theme $theme
45 * The theme to use for discovery.
46 * @param string|bool $subdir
47 * The plugin's subdirectory, for example Plugin/views/filter.
48 * @param string|null $plugin_interface
49 * (optional) The interface each plugin should implement.
50 * @param string $plugin_definition_annotation_name
51 * (optional) Name of the annotation that contains the plugin definition.
52 * Defaults to 'Drupal\Component\Annotation\Plugin'.
54 public function __construct(Theme $theme, $subdir, $plugin_interface = NULL, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') {
55 // Get the active theme.
56 $this->theme = $theme;
58 // Determine the namespaces to search for.
60 foreach ($theme->getAncestry() as $ancestor) {
61 $namespaces['Drupal\\' . $ancestor->getName()] = [DRUPAL_ROOT . '/' . $ancestor->getPath() . '/src'];
63 $this->namespaces = new \ArrayObject($namespaces);
65 $this->subdir = $subdir;
66 $this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name;
67 $this->pluginInterface = $plugin_interface;
68 $this->themeHandler = \Drupal::service('theme_handler');
69 $this->themeManager = \Drupal::service('theme.manager');
75 protected function alterDefinitions(&$definitions) {
76 if ($this->alterHook) {
77 $this->themeManager->alter($this->alterHook, $definitions);
84 public function createInstance($plugin_id, array $configuration = array()) {
85 if (!isset($configuration['theme'])) {
86 $configuration['theme'] = $this->theme;
88 return parent::createInstance($plugin_id, $configuration);
92 * Retrieves the cache tags used to invalidate caches.
95 * An indexed array of cache tags.
97 public function getCacheTags() {
98 return [Bootstrap::CACHE_TAG];
104 public function getDefinitions($sorted = TRUE) {
105 $definitions = parent::getDefinitions();
107 uasort($definitions, ['\Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
113 * Retrieves all definitions where the plugin ID matches a certain criteria.
115 * @param string $regex
116 * The regex pattern to match.
119 * An array of plugin definitions (empty array if no definitions were
120 * found). Keys are plugin IDs.
122 public function getDefinitionsLike($regex) {
124 foreach ($this->getDefinitions() as $plugin_id => $definition) {
125 if (preg_match($regex, $plugin_id)) {
126 $definitions[$plugin_id] = $definition;
129 ksort($definitions, SORT_NATURAL);
136 protected function providerExists($provider) {
137 return $this->themeHandler->themeExists($provider);