Dcp'15

Post on 13-Apr-2017

328 views 0 download

Transcript of Dcp'15

Drupal 8:The Plugin System

Ajit Shinde@azeets

AjitSSr. 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 FormattersField WidgetsFiltersImage EffectsMailContextual LinksLocal ActionsLocal TasksQuickeditEditorsSearch

Tool TipsViews AccessViews ArgumentDefaultViews ArgumentDefaultsViews ArgumentValidatorsViews cacheViews DisplayExtenders

ActionsArchiversBlocksCKEditorPluginsConditionsEditorsEntityReferenceSelectorsField Types

ActionsArchiversBlocksCKEditorPluginsConditionsEditorsEntityReferenceSelectorsField Types

EXISTENCE IN DRUPAL7

• ctools module provided a way to define plugins

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

PLUGIN ECOSYSTEM

DISCOVERY MANAGER

MAPPERSDISCOVERY 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’, $namespaces, 'Drupal\block\Annotation\Block');

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!