Drupal 8 Plugin API

320 views

Published on

Slides for my presentation on Drupal Talks Kharkiv #3
Video: https://www.youtube.com/watch?v=Y9Uyh7_Cyt0&index=3&list=PL_vNb2bJi_TPS0wugsO6ZoAD22dhaWcYj

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
320
On SlideShare
0
From Embeds
0
Number of Embeds
222
Actions
Shares
0
Downloads
1
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Drupal 8 Plugin API

  1. 1. HELLO!
  2. 2. I'MALEX
  3. 3. ZVIRYATKO
  4. 4. 5YRSAT
  5. 5. DRUPAL8 PLUGINAPI
  6. 6. WHATISPLUGININD8? Did you remember ctools? Or any hook_info? block, eld widget, formatter, form element, action, etc... even cache backend.
  7. 7. src/Plugin/Block/SimpleMessageBlock.php /**  * Defines a block with simple message.  *  * @Block(  *   id = "simple_message",  *   admin_label = @Translation("Simple Message"),  * )  */ class SimpleMessageBlock extends BlockBase {   /**    * {@inheritdoc}    */   public function build() {     return [       'message' => [         '#markup' => $this­>t('Just a simple message.'),       ],     ];   } }
  8. 8. OK!BUTHOWTOADD CUSTOMPLUGIN? EASIERTHANIND7
  9. 9. WHERESHOULDIPUTMY FILES? All plugins should be placed in src/Plugin/{PLUGIN_NAME}directory*
  10. 10. HOWTODEFINE? USEANNOTATIONS /**  * @Block(  *   id = "simple_message",  *   admin_label = @Translation("Simple Message"),  * )  */
  11. 11. WHATISTHECODE? ITDEPENDSONINTERFACE class SimpleMessageBlock extends BlockBase {   /**    * {@inheritdoc}    */   public function build() {     return [       'message' => [         '#markup' => $this­>t('Just a simple message.'),       ],     ];   } }
  12. 12. LOOKSSIMPLE ISITALL? NO!
  13. 13. WHATISDERIVATIVES? DYNAMICPLUGINS foreach (module_implements('block_info') as $module) {   $module_blocks = module_invoke($module, 'block_info');   $blocks[$module] = $module_blocks; }
  14. 14. DERIVATIVEEXAMPLE core/modules/system/src/Plugin/Block/SystemMenuBlock.php /**  * @Block(  *   id = "system_menu_block",  *   admin_label = @Translation("Menu"),  *   category = @Translation("Menus"),  *   deriver = "DrupalsystemPluginDerivativeSystemMenuBlock"  * )  */ class SystemMenuBlock extends BlockBase {     // ... } This block is being used for all site menus
  15. 15. BUTHOWITWORKS? Check the deriverannotation property deriver = "DrupalsystemPluginDerivativeSystemMenuBlock" core/modules/system/src/Plugin/Derivative/SystemMenuBlock.ph class SystemMenuBlock extends DeriverBase {   // ...   public function getDerivativeDefinitions($base_definition) {     $blocks = $this­>menuStorage­>loadMultiple();     foreach ($blocks as $menu => $entity) {       $this­>derivatives[$menu] = $base_definition;     }     return $this­>derivatives;   } }
  16. 16. WHATISPLUGINMANAGER? Fat replacement of hook_*_infosystem Plugin manager is responsible for: Plugin discovery (annotation, yaml) Plugin creation (factory)
  17. 17. WHYDOYOUNEEDIT? Create extendable architecture example: layouts you can provide layouts from theme or module and have not be hard linked to main module
  18. 18. HOWTOADD mymodule/mymodule.services.yml services:   plugin.manager.sandwich:     class: DrupalmymoduleSandwichPluginManager     parent: default_plugin_manager
  19. 19. mymodule/src/SandwichPluginManager.php class SandwichPluginManager extends DefaultPluginManager {   public function __construct(      Traversable $namespaces,      CacheBackendInterface $cache_backend,      ModuleHandlerInterface $module_handler   ) {     parent::__construct(       'Plugin/Sandwich', // subdir where to search plugins       $namespaces,       $module_handler,       'DrupalmymoduleSandwichInterface', // strict interface       'DrupalCoreAnnotationPlugin' // annotation class     );     $this­>alterInfo('sandwich_info');     $this­>setCacheBackend($cache_backend, 'sandwich_info');   } }
  20. 20. HOWTOUSE Get a list of available plugins: $type = Drupal::service('plugin.manager.sandwich'); /**  * @var DrupalmymoduleSandwichInterface[] $plugins  */ $plugins = $type­>getDefinitions(); In this case you will receive only objects that implement your interface
  21. 21. PLUGINDISCOVERY ANNOTATION YAML STATIC HOOK
  22. 22. ANNOTATIONDISCOVERY You already saw it /**  * @Block(  *   id = "system_menu_block",  *   admin_label = @Translation("Menu"),  * )  */ This is much better then ctools $plugin = array(); Using in plugin manager new AnnotatedClassDiscovery(   "Plugin/Sandwich", // Subdirectory   $namespaces, // Service "container.namespaces"   "DrupalCoreBlockAnnotationBlock" // Annotation @Block );
  23. 23. YAMLDISCOVERY Usefull for theme plugins, like breakpoints or layouts core/themes/bartik/bartik.breakpoints.yml bartik.wide:   label: wide   mediaQuery: 'all and (min­width: 851px)'   multipliers:     ­ 1x DrupalbreakpointBreakpointManager::$default contains all possible keys $directories = $moduleHandler­>getModuleDirectories() +         $themeHandler­>getThemeDirectories(); new YamlDiscovery('breakpoints', $directories);
  24. 24. STATICDISCOVERY Useful when you need to add 3rd-party class as plugin class SandwichPluginManager extends DefaultPluginManager {   protected function getDiscovery() {     $this­>discovery = new StaticDiscovery();     $this­>discovery­>setDefinition('cheese_sandwich', [       'label' => new TranslatableMarkup('Cheese Sandwich'),       'class' => 'SomeVendorLibraryCheeseSandwich',     ]);     return $this­>discovery;   } }
  25. 25. HOOKDISCOVERY Good old hook_info() class SandwichPluginManager extends DefaultPluginManager {   protected function getDiscovery() {     $this­>discovery = new HookDiscovery(       $this­>moduleHandler,       "sandwich_info"     );     return $this­>discovery;   } } For hook_info_alter()use this $this­>alterInfo("sandwich_info");
  26. 26. DISCOVERYDECORATORS Combine them all together with decorators $d = new AnnotatedClassDiscovery("Plugin/sandwich", $nmspaces); $d = new ContainerDerivativeDiscoveryDecorator($d); $d = new YamlDiscoveryDecorator($d, 'sandwich', $directories); $d = new InfoHookDecorator($d, 'sandwich_info'); $d = new StaticDiscoveryDecorator($d, "callback");
  27. 27. ISITALLNOW?
  28. 28. THANX! presentation drupal.org/u/zviryatko github.com/zviryatko sanya.davyskiba@gmail.com goo.gl/ynFqVo QUESTIONS?

×