StylePluginManager.php in Styles API 8
File
src/Plugin/Style/StylePluginManager.php
View source
<?php
namespace Drupal\styles_api\Plugin\Style;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\YamlDiscoveryDecorator;
class StylePluginManager extends DefaultPluginManager implements StylePluginManagerInterface {
protected $themeHandler;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
$plugin_interface = 'Drupal\\styles_api\\Plugin\\Style\\StyleInterface';
$plugin_definition_annotation_name = 'Drupal\\styles_api\\Annotation\\Style';
parent::__construct("Plugin/Style", $namespaces, $module_handler, $plugin_interface, $plugin_definition_annotation_name);
$discovery = $this
->getDiscovery();
$this->discovery = new YamlDiscoveryDecorator($discovery, 'themes', $module_handler
->getModuleDirectories() + $theme_handler
->getThemeDirectories());
$this->themeHandler = $theme_handler;
$this
->setCacheBackend($cache_backend, 'styles');
$this
->alterInfo('styles');
}
protected function providerExists($provider) {
return $this->moduleHandler
->moduleExists($provider) || $this->themeHandler
->themeExists($provider);
}
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
if ($this->moduleHandler
->moduleExists($definition['provider'])) {
$definition['provider_type'] = 'module';
$base_path = $this->moduleHandler
->getModule($definition['provider'])
->getPath();
}
elseif ($this->themeHandler
->themeExists($definition['provider'])) {
$definition['provider_type'] = 'theme';
$base_path = $this->themeHandler
->getTheme($definition['provider'])
->getPath();
}
else {
$base_path = '';
}
$definition['configuration']['path'] = !empty($definition['configuration']['path']) ? $base_path . '/' . $definition['configuration']['path'] : $base_path;
if (!empty($definition['icon'])) {
$definition['icon'] = $definition['path'] . '/' . $definition['icon'];
}
}
public function getStyleOptions(array $params = []) {
$plugins = $this
->getDefinitions();
$options = array();
foreach ($plugins as $id => $plugin) {
if ($group_by_category) {
$category = isset($plugin['category']) ? (string) $plugin['category'] : 'default';
if (!isset($options[$category])) {
$options[$category] = array();
}
$options[$category][$id] = $plugin['label'];
}
else {
$options[$id] = $plugin['label'];
}
}
return $options;
}
public function getThemeImplementations() {
$plugins = $this
->getDefinitions();
$theme_registry = [];
foreach ($plugins as $id => $definition) {
if (!empty($definition['configuration']['path'])) {
$theme_registry[$id] = $definition['configuration'];
}
}
return $theme_registry;
}
}