SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Downloaden Sie, um offline zu lesen
HELLO!
I'MALEX
ZVIRYATKO
5YRSAT
DRUPAL8
PLUGINAPI
WHATISPLUGININD8?
Did you remember ctools? Or any hook_info?
block, eld widget, formatter, form element, action, etc...
even cache backend.
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.'),
      ],
    ];
  }
}
OK!BUTHOWTOADD
CUSTOMPLUGIN?
EASIERTHANIND7
WHERESHOULDIPUTMY
FILES?
All plugins should be placed in
src/Plugin/{PLUGIN_NAME}directory*
HOWTODEFINE?
USEANNOTATIONS
/**
 * @Block(
 *   id = "simple_message",
 *   admin_label = @Translation("Simple Message"),
 * )
 */
WHATISTHECODE?
ITDEPENDSONINTERFACE
class SimpleMessageBlock extends BlockBase {
  /**
   * {@inheritdoc}
   */
  public function build() {
    return [
      'message' => [
        '#markup' => $this­>t('Just a simple message.'),
      ],
    ];
  }
}
LOOKSSIMPLE
ISITALL?
NO!
WHATISDERIVATIVES?
DYNAMICPLUGINS
foreach (module_implements('block_info') as $module) {
  $module_blocks = module_invoke($module, 'block_info');
  $blocks[$module] = $module_blocks;
}
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
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;
  }
}
WHATISPLUGINMANAGER?
Fat replacement of hook_*_infosystem
Plugin manager is responsible for:
Plugin discovery (annotation, yaml)
Plugin creation (factory)
WHYDOYOUNEEDIT?
Create extendable architecture
example: layouts
you can provide layouts from theme or module
and have not be hard linked to main module
HOWTOADD
mymodule/mymodule.services.yml
services:
  plugin.manager.sandwich:
    class: DrupalmymoduleSandwichPluginManager
    parent: default_plugin_manager
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');
  }
}
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
PLUGINDISCOVERY
ANNOTATION
YAML
STATIC
HOOK
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
);
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);
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;
  }
}
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");
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");
ISITALLNOW?
THANX!
presentation
drupal.org/u/zviryatko
github.com/zviryatko
sanya.davyskiba@gmail.com
goo.gl/ynFqVo
QUESTIONS?

Weitere ähnliche Inhalte

Ähnlich wie Drupal 8 Plugin API

OSGi framework overview
OSGi framework overviewOSGi framework overview
OSGi framework overviewBalduran Chang
 
Custom gutenberg block development in react
Custom gutenberg block development in reactCustom gutenberg block development in react
Custom gutenberg block development in reactImran Sayed
 
The Beauty and the Beast
The Beauty and the BeastThe Beauty and the Beast
The Beauty and the BeastBastian Feder
 
Developing Information Schema Plugins
Developing Information Schema PluginsDeveloping Information Schema Plugins
Developing Information Schema PluginsMark Leith
 
Oracle - Program with PL/SQL - Lession 12
Oracle - Program with PL/SQL - Lession 12Oracle - Program with PL/SQL - Lession 12
Oracle - Program with PL/SQL - Lession 12Thuan Nguyen
 
Whmcs addon module docs
Whmcs addon module docsWhmcs addon module docs
Whmcs addon module docsquyvn
 
Effective Python Package Management [PyCon Canada 2017]
Effective Python Package Management [PyCon Canada 2017]Effective Python Package Management [PyCon Canada 2017]
Effective Python Package Management [PyCon Canada 2017]Devon Bernard
 
Custom gutenberg block development with React
Custom gutenberg block development with ReactCustom gutenberg block development with React
Custom gutenberg block development with ReactImran Sayed
 
The beautyandthebeast phpbat2010
The beautyandthebeast phpbat2010The beautyandthebeast phpbat2010
The beautyandthebeast phpbat2010Bastian Feder
 
The Flexibility of Drupal 8 | DCNLights 2017
The Flexibility of Drupal 8 | DCNLights 2017The Flexibility of Drupal 8 | DCNLights 2017
The Flexibility of Drupal 8 | DCNLights 2017Michael Miles
 
General Principles of Web Security
General Principles of Web SecurityGeneral Principles of Web Security
General Principles of Web Securityjemond
 
The cost of learning - advantage of mixer2
The cost of learning - advantage of mixer2The cost of learning - advantage of mixer2
The cost of learning - advantage of mixer2Y Watanabe
 
7 reasons why developers should love Joomla!
7 reasons why developers should love Joomla!7 reasons why developers should love Joomla!
7 reasons why developers should love Joomla!Bartłomiej Krztuk
 
Struts database access
Struts database accessStruts database access
Struts database accessAbass Ndiaye
 
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres OpenBruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres OpenPostgresOpen
 

Ähnlich wie Drupal 8 Plugin API (20)

OSGi framework overview
OSGi framework overviewOSGi framework overview
OSGi framework overview
 
Drupal 8 Hooks
Drupal 8 HooksDrupal 8 Hooks
Drupal 8 Hooks
 
Alfredo-PUMEX
Alfredo-PUMEXAlfredo-PUMEX
Alfredo-PUMEX
 
Alfredo-PUMEX
Alfredo-PUMEXAlfredo-PUMEX
Alfredo-PUMEX
 
Alfredo-PUMEX
Alfredo-PUMEXAlfredo-PUMEX
Alfredo-PUMEX
 
Alfredo-PUMEX
Alfredo-PUMEXAlfredo-PUMEX
Alfredo-PUMEX
 
Custom gutenberg block development in react
Custom gutenberg block development in reactCustom gutenberg block development in react
Custom gutenberg block development in react
 
The Beauty and the Beast
The Beauty and the BeastThe Beauty and the Beast
The Beauty and the Beast
 
Developing Information Schema Plugins
Developing Information Schema PluginsDeveloping Information Schema Plugins
Developing Information Schema Plugins
 
Oracle - Program with PL/SQL - Lession 12
Oracle - Program with PL/SQL - Lession 12Oracle - Program with PL/SQL - Lession 12
Oracle - Program with PL/SQL - Lession 12
 
Whmcs addon module docs
Whmcs addon module docsWhmcs addon module docs
Whmcs addon module docs
 
Effective Python Package Management [PyCon Canada 2017]
Effective Python Package Management [PyCon Canada 2017]Effective Python Package Management [PyCon Canada 2017]
Effective Python Package Management [PyCon Canada 2017]
 
Custom gutenberg block development with React
Custom gutenberg block development with ReactCustom gutenberg block development with React
Custom gutenberg block development with React
 
The beautyandthebeast phpbat2010
The beautyandthebeast phpbat2010The beautyandthebeast phpbat2010
The beautyandthebeast phpbat2010
 
The Flexibility of Drupal 8 | DCNLights 2017
The Flexibility of Drupal 8 | DCNLights 2017The Flexibility of Drupal 8 | DCNLights 2017
The Flexibility of Drupal 8 | DCNLights 2017
 
General Principles of Web Security
General Principles of Web SecurityGeneral Principles of Web Security
General Principles of Web Security
 
The cost of learning - advantage of mixer2
The cost of learning - advantage of mixer2The cost of learning - advantage of mixer2
The cost of learning - advantage of mixer2
 
7 reasons why developers should love Joomla!
7 reasons why developers should love Joomla!7 reasons why developers should love Joomla!
7 reasons why developers should love Joomla!
 
Struts database access
Struts database accessStruts database access
Struts database access
 
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres OpenBruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
 

Kürzlich hochgeladen

New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 

Kürzlich hochgeladen (20)

New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 

Drupal 8 Plugin API