Dcp'15

30
Drupal 8: The Plugin System

Transcript of Dcp'15

Page 1: Dcp'15

Drupal 8:The Plugin System

Page 2: Dcp'15

Ajit Shinde@azeets

AjitSSr. Drupal Developer

Piyuesh Kumar@piyuesh23

Drupal Architect

Page 3: Dcp'15

• What are Plugins?

Benefits, why use them?

Existence in D7

Plugin Eco-system

Plugin Manager

Demo Plugin Manager

AGENDA

Page 4: Dcp'15

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?

Page 5: Dcp'15

Design-Pattern

A general reusable solution to a recurring problem within a given context.

What actually is a Plugin?

Page 6: Dcp'15

• Discovery

Decorators

Factory

Mapper

Manager

TERMINOLOGIES

Page 7: Dcp'15

• 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?

Page 8: Dcp'15

Plugin Types in Core

Field FormattersField WidgetsFiltersImage EffectsMailContextual LinksLocal ActionsLocal TasksQuickeditEditorsSearch

Tool TipsViews AccessViews ArgumentDefaultViews ArgumentDefaultsViews ArgumentValidatorsViews cacheViews DisplayExtenders

ActionsArchiversBlocksCKEditorPluginsConditionsEditorsEntityReferenceSelectorsField Types

ActionsArchiversBlocksCKEditorPluginsConditionsEditorsEntityReferenceSelectorsField Types

Page 9: Dcp'15

EXISTENCE IN DRUPAL7

• ctools module provided a way to define plugins

• e.g. content type plugin, context plugin, etc.

Page 10: Dcp'15

PLUGIN ECOSYSTEM

DISCOVERY MANAGER

MAPPERSDISCOVERY DECORATORS

FACTORIES

PLUGINS

Page 11: Dcp'15

PLUGIN MANAGER

• Discovery• Instantiation• Functionality• Grouping

Page 12: Dcp'15

PLUGIN MANAGER

PluginManagerBase

PluginManagerInterface

DiscoveryInterface FactoryInterface MapperInterface

Implements

Extends

Page 13: Dcp'15

PLUGIN MANAGER

• Extend DefaultPluginManager + use defaults

• Extend DefaultPluginManager

• Override DefaultPluginManager::discovery()

• Override DefaultPluginManager::factory()

OR

Page 14: Dcp'15

HELPER FUNCTIONS

• getDefinition/s()

• getDiscovery()

• getFactory()

• createInstance()

• getInstance()

Page 15: Dcp'15

PLUGIN DISCOVERY

• Annotated Plugin Discovery

• Hook Based Plugin Discovery

• YAML Based Plugin Discovery

• Static Plugin Discovery

Page 16: Dcp'15

ANNOTATED PLUGIN DISCOVERY

• Provided by AnnotatedClassDiscovery class

• Follows PSR-4 standard

• Metadata is read from the @docblock

MyPluginManager::discovery = new AnnotatedClassDiscovery(‘Plugin/Block’, $namespaces, 'Drupal\block\Annotation\Block');

Page 17: Dcp'15

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');

Page 18: Dcp'15

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"

Page 19: Dcp'15

STATIC PLUGIN DISCOVERY

• Provided by StaticDiscovery class

• Manually register plugin definition

• Mainly used in tests

MyPluginManager::discovery = new StaticDiscovery();

Page 20: Dcp'15

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

Page 21: Dcp'15

INFOHOOK DECORATOR

• Adds processing for plugins exposed via info hooks.

MyPluginManager::discovery = new InfoHookDecorator(new HookDiscovery('block_info'), 'block_info_alter');

Page 22: Dcp'15

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'));

Page 23: Dcp'15

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

Page 24: Dcp'15

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);

Page 25: Dcp'15

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);

Page 26: Dcp'15

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

Page 27: Dcp'15

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.

Page 28: Dcp'15

TIME FOR SOME FUN

Page 29: Dcp'15

METAPHOR

Page 30: Dcp'15

THANK YOU!