D8: Plugins

Photo by Greg Rakozy on UnsplashPhoto by Adam Birkett on Unsplash

Everywhere!

$ drupal list | grep plugin | wc -l
21

$ drupal dpl | wc -l
53

?

Define

Plugins used to provide a new object of specific type

Entity   -> FieldType
         -> FieldWidget
         -> FieldFormatter
...

Views    -> Display
         -> Field
         -> Filter
...

Typed
Data     -> Data types

Render 
Elements -> RenderElement
         -> FormElement
......

Plugins designed as easy to embed objects into system

Write meta-information

define()

Plug & Play

/**
 * @DataType(
 *   id = "list",
 *   label = @Translation("List of items"),
 *   definition_class = "\Drupal\Core\TypedData\ListDataDefinition"
 * )
 */
class ItemList extends TypedData ... {
  ...
}

Place it in a magic 🔮 folder

example: MY_MODULE/src/Plugin/DataType

1

2

Plugins always needs a manager

define()

The Law of Demeter?

class DefaultPluginManager extends PluginManagerBase 
implements PluginManagerInterface, ... {
  ...
}
  
interface PluginManagerInterface extends 
  DiscoveryInterface, 
  FactoryInterface,
  MapperInterface{
}

interface DiscoveryInterface {
  getDefinition($plugin_id, $exception_on_invalid = TRUE);
  getDefinitions();
  hasDefinition($plugin_id);
}

interface FactoryInterface {
  createInstance($plugin_id, array $configuration = []);
}

interface MapperInterface {
  getInstance(array $options);
}

Each type of plugin use different interface
USB, HDMI, ThunderBolt, 3mm jack?

define()

Interface

FieldType -> Drupal\Core\Field\FieldItemInterface

-> schema()

-> preSave()

-> delete()

....

FieldFormatter -> Drupal\Core\Field\FormatterInterface

-> settingsForm

-> view()

....

😉 HDMI?

😉 USB?

Photo by Greg Rakozy on Unsplash

  • Own plugin types
  • Plugin collections
  • Derivatives
  • Plugins with context \Drupal\condition_test\Plugin\Condition\ConditionTestDualUser

Advanced

Links

Founder @

Co- Founder @

Vladyslav Moyseenko, a.k.a vlad.dancer

&

D8 School: Plugins

By Vlad Moyseenko

D8 School: Plugins

  • 548