Drupal 8:
The Plugin System
Ajit Shinde
@azeets
AjitS
Sr. Drupal Developer
Piyuesh Kumar
@piyuesh23
Drupal Architect
• What are Plugins?
Benefits, why use them?
Existence in D7
Plugin Eco-system
Plugin Manager
Demo Plugin Manager
AGENDA
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?
Design-Pattern
A general reusable solution to a recurring
problem within a given context.
What actually is a Plugin?
• Discovery
Decorators
Factory
Mapper
Manager
TERMINOLOGIES
• 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?
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
EXISTENCE IN DRUPAL7
• ctools module provided a way to define
plugins
• e.g. content type plugin, context plugin, etc.
PLUGIN ECOSYSTEM
DISCOVERY MANAGER
MAPPERS
DISCOVERY
DECORATORS
FACTORIES
PLUGINS
PLUGIN MANAGER
• Discovery
• Instantiation
• Functionality
• Grouping
PLUGIN MANAGER
PluginManagerBase
PluginManagerInterface
DiscoveryInterface FactoryInterface MapperInterface
Implements
Extends
PLUGIN MANAGER
• Extend DefaultPluginManager + use defaults
• Extend DefaultPluginManager
• Override DefaultPluginManager::discovery()
• Override DefaultPluginManager::factory()
OR
HELPER FUNCTIONS
• getDefinition/s()
• getDiscovery()
• getFactory()
• createInstance()
• getInstance()
PLUGIN DISCOVERY
• Annotated Plugin Discovery
• Hook Based Plugin Discovery
• YAML Based Plugin Discovery
• Static Plugin Discovery
ANNOTATED PLUGIN DISCOVERY
• Provided by AnnotatedClassDiscovery class
• Follows PSR-4 standard
• Metadata is read from the @docblock
MyPluginManager::discovery = new AnnotatedClassDiscovery(‘Plugin/Block’, $namespace
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');
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"
STATIC PLUGIN DISCOVERY
• Provided by StaticDiscovery class
• Manually register plugin definition
• Mainly used in tests
MyPluginManager::discovery = new StaticDiscovery();
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
INFOHOOK DECORATOR
• Adds processing for plugins exposed via info hooks.
MyPluginManager::discovery = new InfoHookDecorator(
new HookDiscovery('block_info'), 'block_info_alter');
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'));
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
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);
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);
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
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.
TIME FOR SOME FUN
METAPHOR
THANK YOU!

Dcp'15

  • 1.
  • 2.
    Ajit Shinde @azeets AjitS Sr. DrupalDeveloper Piyuesh Kumar @piyuesh23 Drupal Architect
  • 3.
    • What arePlugins? Benefits, why use them? Existence in D7 Plugin Eco-system Plugin Manager Demo Plugin Manager AGENDA
  • 4.
    Something that providesa 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?
  • 5.
    Design-Pattern A general reusablesolution to a recurring problem within a given context. What actually is a Plugin?
  • 6.
  • 7.
    • Not tiedwith 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 inCore 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.
  • 10.
  • 11.
    PLUGIN MANAGER • Discovery •Instantiation • Functionality • Grouping
  • 12.
  • 13.
    PLUGIN MANAGER • ExtendDefaultPluginManager + use defaults • Extend DefaultPluginManager • Override DefaultPluginManager::discovery() • Override DefaultPluginManager::factory() OR
  • 14.
    HELPER FUNCTIONS • getDefinition/s() •getDiscovery() • getFactory() • createInstance() • getInstance()
  • 15.
    PLUGIN DISCOVERY • AnnotatedPlugin Discovery • Hook Based Plugin Discovery • YAML Based Plugin Discovery • Static Plugin Discovery
  • 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 PLUGINDISCOVERY • 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 PLUGINDISCOVERY • 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 • Wrapperaround 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 • Addsprocessing for plugins exposed via info hooks. MyPluginManager::discovery = new InfoHookDecorator( new HookDiscovery('block_info'), 'block_info_alter');
  • 22.
    Discovery Decorators • Providedby 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 • Providesplugin 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 • Basicfactory 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 • Extendsthe 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 • Providedby 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 • Providean 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.
  • 28.
  • 29.
  • 30.