3. • What are Plugins?
Benefits, why use them?
Existence in D7
Plugin Eco-system
Plugin Manager
Demo Plugin Manager
AGENDA
4. Something that provides a set of guidelines and
reusable code components to allow developers
to expose pluggable components within their
code and (as needed) support managing these
components through the user interface.
What is a Plugin?
7. • Not tied with Drupal
Lazy loaded
Extensible because of the use of object oriented
programming concept : class
Interface driven
Does only one thing at a time
Swappable
Why Plugins / Benefits?
8. Plugin Types in Core
Field Formatters
Field Widgets
Filters
Image Effects
Mail
Contextual Links
Local Actions
Local Tasks
Quickedit
Editors
Search
Tool Tips
Views Access
Views Argument
Default
Views Argument
Defaults
Views Argument
Validators
Views cache
Views Display
Extenders
Actions
Archivers
Blocks
CKEditor
Plugins
Conditions
Editors
Entity
Reference
Selectors
Field Types
Actions
Archivers
Blocks
CKEditor
Plugins
Conditions
Editors
Entity
Reference
Selectors
Field Types
9. EXISTENCE IN DRUPAL7
• ctools module provided a way to define
plugins
• e.g. content type plugin, context plugin, etc.
16. ANNOTATED PLUGIN DISCOVERY
• Provided by AnnotatedClassDiscovery class
• Follows PSR-4 standard
• Metadata is read from the @docblock
MyPluginManager::discovery = new AnnotatedClassDiscovery(‘Plugin/Block’, $namespace
17. HOOK BASED PLUGIN DISCOVERY
• Provided by HookDiscovery class
• Discovery based on the info hooks as done in
Drupal 7
• Mainly present to provide backwards compatibility
• Eg. The following provides a
{module_name}_block_info in the module file
MyPluginManager::discovery = new HookDiscovery($this->moduleHandler, 'block_info');
18. YAML BASED PLUGIN DISCOVERY
• Provided by YamlDiscovery class
• Using the YML file located at the module root to
read the metadata
MyPluginManager::discovery = new YamlDiscovery('blocks',
$module_handler->getModuleDirectories());
E.g. mymodule.blocks.yml
block:
id: "block_id"
admin_label: "Label for user interface"
category: "Category of the block"
19. STATIC PLUGIN DISCOVERY
• Provided by StaticDiscovery class
• Manually register plugin definition
• Mainly used in tests
MyPluginManager::discovery = new StaticDiscovery();
20. Discovery Decorators
• Wrapper around discovery class
• The decorator wraps around a DiscoveryInterface to
provide additional functionality.
• E.g. For altering a the info given by the block and
defining a {mymodule}_block_info_alter
• Types:
• InfoHookDecorator
• Derivative Discovery Decorator
21. INFOHOOK DECORATOR
• Adds processing for plugins exposed via info hooks.
MyPluginManager::discovery = new InfoHookDecorator(
new HookDiscovery('block_info'), 'block_info_alter');
22. Discovery Decorators
• Provided by the DerivativeDiscoveryDecorator class
• Allows variable number of plugins based on
configuration or application state.
• Eg. Menu and the corresponding blocks
MyPluginManager::discovery = new DerivativeDiscoveryDecorator(
new HookDiscovery('block_info'));
23. PLUGIN FACTORIES
• Provides plugin manager with ::createInstance()
method
• Routes the request to the selected factory class
• Instantiates and returns the specific plugin instance
• Types:
• DefaultFactory
• ContainerFactory
• ReflectionFactory
24. DEFAULT FACTORY
• Basic factory defined as the DefaultFactory class
• Looks for the plugin class and arguments required,
instantiates the plugin and returns it
return new $plugin_class($configuration, $plugin_id, $plugin_definition);
25. CONTAINER FACTORY
• Extends the DefaultFactory class, with an additional
method ::create()
• Used to inject the service container, so that the
plugin could make use of it
• If service container is not passed, works as the
default factory.
return $plugin_class::create(Drupal::getContainer(), $configuration,
$plugin_id, $plugin_definition);
26. REFLECTION FACTORY
• Provided by the ReflectionFactory class.
• Provides a way to instantiate the by calling the
appropriate constructor with correct arguments.
• Provides an abstraction layer.
$arguments = $this->getInstanceArguments($reflector, $plugin_id,
$plugin_definition, $configuration);
$instance = $reflector->newInstanceArgs($arguments);
return $instance
27. PLUGIN MAPPERS
• Provide an additional layer of abstraction around
plugin factory.
• Allow extra processing before instantiating the
plugin
• Similar to the decorators in the plugin discovery.
• Eg. Cache objects with different caching
mechanisms, RESTFUL services.