By duadua on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
10.2.x
Introduced in version:
10.2.0
Description:
Related change record: https://www.drupal.org/node/3395575 for plugins
Convert a plugin type
- Introduce Attribute object
- Provide a 1to1 mapping between the annotation keys and attribute parameter names
- Update constructor of plugin manager and pass along the new attribute class, followed by the Annotation class for backwards compatibility
- DefaultPluginManager::__construct() supports 3 cases
- Only an Annnotation class (deprecated, uses AnnotatedClassDiscovery)
- An Attribute class, followed by an Annotation class (recommended for existing plugin types to avoid BC breaks. Uses AttributeDiscoveryWithAnnotations, Drupal 11 will ignore the Annotation class parameter then)
- An Attribute class (recommended for new plugin types, uses AttributeClassDiscovery
- The code examples below use full namespaces for clarity, implementations may use use statements and just Action::class for example.
Before
// Annotation class
class Action extends \Drupal\Component\Annotation\Plugin {
...
}
// Plugin manager:
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/Action', $namespaces, $module_handler, 'Drupal\Core\Action\ActionInterface', 'Drupal\Core\Annotation\Action');
}
After
// Attribute class
#[\Attribute(\Attribute::TARGET_CLASS)]
class Action extends \Drupal\Component\Plugin\Attribute\Plugin {
…
}
// Plugin manager
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/Action', $namespaces, $module_handler, 'Drupal\Core\Action\ActionInterface', 'Drupal\Core\Action\Attribute\Action', 'Drupal\Core\Annotation\Action');
}
Not providing an attribute class is deprecated in Drupal 11.2 and will no longer be supported in Drupal 12.
Impacts:
Module developers