class DesignTokenPluginManager
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/Theme/DesignToken/DesignTokenPluginManager.php \Drupal\Core\Theme\DesignToken\DesignTokenPluginManager
Defines a design tokens plugin manager.
@method \Drupal\Core\Theme\DesignToken\DesignToken|null getDefinition($plugin_id, $exception_on_invalid = TRUE) @method \Drupal\Core\Theme\DesignToken\DesignToken[] getDefinitions()
@internal The design tokens API is experimental and is not meant for production use. See https://www.drupal.org/core/experimental for more information.
Hierarchy
- class \Drupal\Component\Plugin\PluginManagerBase implements \Drupal\Component\Plugin\PluginManagerInterface uses \Drupal\Component\Plugin\Discovery\DiscoveryTrait
- class \Drupal\Core\Plugin\DefaultPluginManager implements \Drupal\Component\Plugin\PluginManagerInterface, \Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface, \Drupal\Core\Cache\CacheableDependencyInterface uses \Drupal\Component\Plugin\Discovery\DiscoveryCachedTrait, \Drupal\Core\Cache\UseCacheBackendTrait extends \Drupal\Component\Plugin\PluginManagerBase
- class \Drupal\Core\Theme\DesignToken\DesignTokenPluginManager implements \Drupal\Core\Theme\DesignToken\DesignTokenPluginManagerInterface extends \Drupal\Core\Plugin\DefaultPluginManager
- class \Drupal\Core\Plugin\DefaultPluginManager implements \Drupal\Component\Plugin\PluginManagerInterface, \Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface, \Drupal\Core\Cache\CacheableDependencyInterface uses \Drupal\Component\Plugin\Discovery\DiscoveryCachedTrait, \Drupal\Core\Cache\UseCacheBackendTrait extends \Drupal\Component\Plugin\PluginManagerBase
Expanded class hierarchy of DesignTokenPluginManager
2 files declare their use of DesignTokenPluginManager
- DesignTokenPluginManagerKernelTest.php in core/
tests/ Drupal/ KernelTests/ Core/ Theme/ DesignToken/ DesignTokenPluginManagerKernelTest.php - DesignTokenPluginManagerTest.php in core/
tests/ Drupal/ Tests/ Core/ Theme/ DesignToken/ DesignTokenPluginManagerTest.php
1 string reference to 'DesignTokenPluginManager'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses DesignTokenPluginManager
File
-
core/
lib/ Drupal/ Core/ Theme/ DesignToken/ DesignTokenPluginManager.php, line 27
Namespace
Drupal\Core\Theme\DesignTokenView source
class DesignTokenPluginManager extends DefaultPluginManager implements DesignTokenPluginManagerInterface {
public function __construct(ModuleHandlerInterface $module_handler, protected readonly ThemeHandlerInterface $themeHandler, CacheBackendInterface $cacheBackend, protected readonly DesignTokenValuePluginManagerInterface $designTokenValuePluginManager) {
$this->moduleHandler = $module_handler;
$this->factory = new ContainerFactory($this);
$this->alterInfo('design_token_info');
$this->setCacheBackend($cacheBackend, 'design_token', [
'design_token',
]);
}
/**
* {@inheritdoc}
*/
public function findDefinitions() : array {
$discovered = parent::findDefinitions();
return $this->extractDefinitions($discovered);
}
/**
* {@inheritdoc}
*/
protected function getDiscovery() : DiscoveryInterface {
if (!$this->discovery) {
$this->discovery = new YamlDiscovery('tokens', $this->moduleHandler
->getModuleDirectories() + $this->themeHandler
->getThemeDirectories());
}
return $this->discovery;
}
/**
* {@inheritdoc}
*/
protected function providerExists(mixed $provider) : bool {
/** @var string $provider */
return $this->moduleHandler
->moduleExists($provider) || $this->themeHandler
->themeExists($provider);
}
/**
* Convert raw plugin definitions from the discovery into a flattened list.
*
* YamlDiscovery doesn't know about the nested groups structure of DTCG and
* may create a plugin by group instead of a plugin by token.
*
* @param array $discovered
* The raw plugin definitions from the discovery.
*
* @return array
* The extracted definitions.
*/
protected function extractDefinitions(array $discovered) : array {
$definitions = [];
foreach ($discovered as $id => $data) {
// Id & provider have been automatically added by YamlDiscovery.
unset($data['id']);
$provider = $data['provider'];
unset($data['provider']);
$tree = $this->parseDtcg($provider, $id, $data);
$definitions += $this->flattenDefinitions($tree);
}
return $definitions;
}
/**
* Parse W3C DTCG format.
*
* @param string $provider
* The Drupal extension providing the token.
* @param string $name
* The token name.
* @param array $data
* The raw DTCG data.
* @param ?\Drupal\Core\Theme\DesignToken\Group $parent
* The parent group. NULL if root.
*
* @return \Drupal\Core\Theme\DesignToken\DtcgInterface|null
* A DTCG object.
*/
protected function parseDtcg(string $provider, string $name, array $data, ?Group $parent = NULL) : ?DtcgInterface {
// phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
$description = isset($data['$description']) ? new TranslatableMarkup($data['$description']) : NULL;
// An array with a $value property is a token.
if (isset($data['$value'])) {
try {
$data['$value'] = DesignTokenValuePluginBase::prepareConfiguration($data['$value']);
/** @var \Drupal\Core\Theme\DesignToken\DesignTokenValueInterface $value */
$value = $this->designTokenValuePluginManager
->createInstance($data['$type'] ?? $parent?->type, $data['$value']);
return new DesignToken($provider, $name, $value, $parent, $description, $data['$type'] ?? NULL);
} catch (PluginException) {
// Type does not have a design token value plugin or has an invalid
// structure.
return NULL;
}
}
// An array without a $value property is a group.
$group = new Group($provider, $name, [], $parent, $description, $data['$type'] ?? NULL);
$children = [];
foreach ($data as $childName => $childData) {
if (str_starts_with($childName, '$')) {
continue;
}
$child = $this->parseDtcg($provider, $childName, $childData, $group);
if ($child) {
$children[$childName] = $child;
}
}
$group->setChildren($children);
return $group;
}
/**
* Get Design Token plugins from a tree of processed DTCG objects.
*
* @param ?\Drupal\Core\Theme\DesignToken\DtcgInterface $dtcgTree
* A DTCG object.
*
* @return array
* The list of design token plugins.
*/
protected function flattenDefinitions(?DtcgInterface $dtcgTree) : array {
$tokens = [];
if ($dtcgTree instanceof DesignToken) {
$tokens[$dtcgTree->id()] = $dtcgTree;
}
elseif ($dtcgTree instanceof Group) {
foreach ($dtcgTree->getChildren() as $child) {
$tokens += $this->flattenDefinitions($child);
}
}
return $tokens;
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
|---|---|---|---|---|---|
| DefaultPluginManager::$additionalAnnotationNamespaces | protected | property | Additional annotation namespaces. | ||
| DefaultPluginManager::$alterHook | protected | property | Name of the alter hook if one should be invoked. | ||
| DefaultPluginManager::$cacheKey | protected | property | The cache key. | ||
| DefaultPluginManager::$cacheTags | protected | property | An array of cache tags to use for the cached definitions. | ||
| DefaultPluginManager::$defaults | protected | property | A set of defaults to be referenced by $this->processDefinition(). | 11 | |
| DefaultPluginManager::$moduleExtensionList | protected | property | The module extension list. | ||
| DefaultPluginManager::$moduleHandler | protected | property | The module handler to invoke the alter hook. | 1 | |
| DefaultPluginManager::$namespaces | protected | property | An object of root paths that are traversable. | ||
| DefaultPluginManager::$pluginDefinitionAnnotationName | protected | property | The name of the annotation that contains the plugin definition. | ||
| DefaultPluginManager::$pluginDefinitionAttributeName | protected | property | The name of the attribute that contains the plugin definition. | ||
| DefaultPluginManager::$pluginInterface | protected | property | The interface each plugin should implement. | 1 | |
| DefaultPluginManager::$subdir | protected | property | The subdirectory within a namespace to look for plugins. | ||
| DefaultPluginManager::alterDefinitions | protected | function | Invokes the hook to alter the definitions if the alter hook is set. | 4 | |
| DefaultPluginManager::alterInfo | protected | function | Sets the alter hook name. | ||
| DefaultPluginManager::clearCachedDefinitions | public | function | Clears static and persistent plugin definition caches. | Overrides CachedDiscoveryInterface::clearCachedDefinitions | 9 |
| DefaultPluginManager::extractProviderFromDefinition | protected | function | Extracts the provider from a plugin definition. | ||
| DefaultPluginManager::getCacheContexts | public | function | The cache contexts associated with this object. | Overrides CacheableDependencyInterface::getCacheContexts | |
| DefaultPluginManager::getCachedDefinitions | protected | function | Returns the cached plugin definitions of the decorated discovery class. | ||
| DefaultPluginManager::getCacheMaxAge | public | function | The maximum age for which this object may be cached. | Overrides CacheableDependencyInterface::getCacheMaxAge | |
| DefaultPluginManager::getCacheTags | public | function | The cache tags associated with this object. | Overrides CacheableDependencyInterface::getCacheTags | |
| DefaultPluginManager::getDefinitions | public | function | Gets the definition of all plugins for this type. | Overrides DiscoveryTrait::getDefinitions | 2 |
| DefaultPluginManager::getFactory | protected | function | Gets the plugin factory. | Overrides PluginManagerBase::getFactory | |
| DefaultPluginManager::processDefinition | public | function | Performs extra processing on plugin definitions. | 16 | |
| DefaultPluginManager::setCacheBackend | public | function | Initialize the cache backend. | ||
| DefaultPluginManager::setCachedDefinitions | protected | function | Sets a cache of plugin definitions for the decorated discovery class. | ||
| DefaultPluginManager::useCaches | public | function | Disable the use of caches. | Overrides CachedDiscoveryInterface::useCaches | 1 |
| DesignTokenPluginManager::extractDefinitions | protected | function | Convert raw plugin definitions from the discovery into a flattened list. | ||
| DesignTokenPluginManager::findDefinitions | public | function | Finds plugin definitions. | Overrides DefaultPluginManager::findDefinitions | |
| DesignTokenPluginManager::flattenDefinitions | protected | function | Get Design Token plugins from a tree of processed DTCG objects. | ||
| DesignTokenPluginManager::getDiscovery | protected | function | Gets the plugin discovery. | Overrides DefaultPluginManager::getDiscovery | |
| DesignTokenPluginManager::parseDtcg | protected | function | Parse W3C DTCG format. | ||
| DesignTokenPluginManager::providerExists | protected | function | Determines if the provider of a definition exists. | Overrides DefaultPluginManager::providerExists | |
| DesignTokenPluginManager::__construct | public | function | Constructs a new \Drupal\Core\Plugin\DefaultPluginManager object. | Overrides DefaultPluginManager::__construct | |
| DiscoveryCachedTrait::$definitions | protected | property | Cached definitions array, or NULL when not initialized. | 1 | |
| DiscoveryCachedTrait::getDefinition | public | function | Overrides DiscoveryTrait::getDefinition | 3 | |
| DiscoveryTrait::doGetDefinition | protected | function | Gets a specific plugin definition. | ||
| DiscoveryTrait::hasDefinition | public | function | |||
| PluginManagerBase::$discovery | protected | property | The object that discovers plugins managed by this manager. | ||
| PluginManagerBase::$factory | protected | property | The object that instantiates plugins managed by this manager. | ||
| PluginManagerBase::$mapper | protected | property | The preconfigured plugin instance for a particular runtime condition. | ||
| PluginManagerBase::createInstance | public | function | Creates a plugin instance based on the provided ID and configuration. | Overrides FactoryInterface::createInstance | 14 |
| PluginManagerBase::getFallbackPluginId | protected | function | Gets a fallback id for a missing plugin. | 6 | |
| PluginManagerBase::getInstance | public | function | Gets or creates a plugin instance that satisfies the given options. | Overrides MapperInterface::getInstance | 6 |
| PluginManagerBase::handlePluginNotFound | protected | function | Allows plugin managers to specify custom behavior if a plugin is not found. | 1 | |
| UseCacheBackendTrait::$cacheBackend | protected | property | Cache backend instance. | ||
| UseCacheBackendTrait::$useCaches | protected | property | Flag whether caches should be used or skipped. | ||
| UseCacheBackendTrait::cacheGet | protected | function | Fetches from the cache backend, respecting the use caches flag. | ||
| UseCacheBackendTrait::cacheSet | protected | function | Stores data in the persistent cache, respecting the use caches flag. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.