Drupal 8 plugin system
aspilicious!
About me
•  Bram Goffings
@aspilicious
•  Drupal developer
•  Work for Nascom
www.nascom.be
•  Maintainer of Display Suite...
Target audience
•  Site builders & module developers
•  Experience with module development
•  Understanding of the hook sy...
Success?
•  Nobody is scared anymore
•  Everyone can make a *basic* plugin type
Not the expert
•  I’m an end user, just like you
•  Learned by trying (and failing)
•  I can say stupid things
Drupal developer
•  Mr. Steven
•  Brave drupal 7 expert
•  Made his first Drupal 8 site
•  Wants to build a plugin
system
Client
•  Mr. Arnold
•  Rich webmaster
•  Loves cars
•  I want to choose my favorite car on my profile from
a predefined list.
•  It should be possible to plug in different ca...
I take the job…
Step 1
I want to choose my favorite car on my profile
from a predefined list.
Arnold’s wireframe
Steven’s plan
•  Add a “Car selector” field
•  Create a custom select widget
The drupal 7 way
•  Implements hook_field_widget_info()
•  Implements hook_field_widget_form()
Those hooks are gone!
They are plugin-y-fied!
What is a plugin?
Plugins are small pieces of functionality that are
swappable.
Plugins that perform similar functionality...
Field widget plugins!
•  There is a “widget” plugin type!
•  Each field widget is from that type!
Structure
SelectWidget.php
namespace DrupaloptionsPluginFieldFieldWidget;
SelectWidget.php
namespace DrupaloptionsPluginFieldFieldWidget;
SelectWidget.php
namespace DrupaloptionsPluginFieldFieldWidget;
SelectWidget.php
namespace DrupaloptionsPluginFieldFieldWidget;
Namespaces
•  Namespaces were introduced in PHP 5.3
•  Used to avoid name collisions
PSR-0
•  Aims to provide a standard file, class and namespace
convention to allow plug-and-play code.
•  Uses the defined ...
SelectWidget.php
namespace DrupaloptionsPluginFieldFieldWidget;
Structure
PSR-4
•  “New standard”
•  Makes alternative mapping possible
•  Drupal will use it to remove “unneeded” folders. The empt...
Let’s recap
•  We know how to find plugins
•  We understand namespaces and the directory
structure
•  We understand PSR-0 ...
SelectWidget.php
Available methods
Car module structure
car.info.yml
Step 1: done
Let’s recap
•  We know how to start with writing a field widget
plugin
•  We know how to start writing a custom module
Step 2
It should be possible to plug in
different cars into the list.
Steven’s plan
•  Create a custom plugin type named “Car”
•  Create two sample car plugins
Custom plugin type
•  Create a plugin manager service record
•  Create an annotation class
•  Create the plugin manager cl...
Custom plugin type
car.services.yml
Car module structure
(part 2)
Sample plugins
Sample plugins
getOptions() v2
Debug output
Car module structure
(part 3)
Let’s recap
•  We know how to write a basic plugin manager
•  We know how to handle annotations
•  We know how to get all ...
Disadvantage
Each car requires a plugin
Derivative
•  Allows you to create multiple plugin definitions
through a single plugin class.
Derivative example!
•  Create a yml file containing cars!
•  Create a derivative plugin!
•  Create a definition for each car...
cars-example.yml
YamlCar.php
Step 2: done
Let’s recap
•  We know when to use derivatives
•  We are able to build one
Step 3
The name and an image of the selected car should
placed on the homepage.
Steven’s plan
•  Add a function to the plugins that returns an image
url
•  Create the frontpage
hook_menu is gone!
car.routing.yml
Car module structure
(part 4)
Let’s recap
•  We figured out how to add a function to a plugin
•  We know how to create a plugin instance
•  We saw how w...
The end
Questions?
THANK YOU!
Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8
Upcoming SlideShare
Loading in …5
×

Dev days Szeged 2014: Plugin system in drupal 8

873 views

Published on

Drupal dev days 2014 presentation in Szeged.
Plugin system in drupal 8.

This is the unchanged version of my slides.

Published in: Software
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
873
On SlideShare
0
From Embeds
0
Number of Embeds
41
Actions
Shares
0
Downloads
11
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Dev days Szeged 2014: Plugin system in drupal 8

  1. 1. Drupal 8 plugin system aspilicious!
  2. 2. About me •  Bram Goffings @aspilicious •  Drupal developer •  Work for Nascom www.nascom.be •  Maintainer of Display Suite •  Drupal 8 core contributor
  3. 3. Target audience •  Site builders & module developers •  Experience with module development •  Understanding of the hook system •  Know how to handle object oriented code
  4. 4. Success? •  Nobody is scared anymore •  Everyone can make a *basic* plugin type
  5. 5. Not the expert •  I’m an end user, just like you •  Learned by trying (and failing) •  I can say stupid things
  6. 6. Drupal developer •  Mr. Steven •  Brave drupal 7 expert •  Made his first Drupal 8 site •  Wants to build a plugin system
  7. 7. Client •  Mr. Arnold •  Rich webmaster •  Loves cars
  8. 8. •  I want to choose my favorite car on my profile from a predefined list. •  It should be possible to plug in different cars into the list. •  The name and an image of the selected car should placed on the homepage.
  9. 9. I take the job…
  10. 10. Step 1 I want to choose my favorite car on my profile from a predefined list.
  11. 11. Arnold’s wireframe
  12. 12. Steven’s plan •  Add a “Car selector” field •  Create a custom select widget
  13. 13. The drupal 7 way •  Implements hook_field_widget_info() •  Implements hook_field_widget_form()
  14. 14. Those hooks are gone!
  15. 15. They are plugin-y-fied!
  16. 16. What is a plugin? Plugins are small pieces of functionality that are swappable. Plugins that perform similar functionality are of the same plugin type.
  17. 17. Field widget plugins! •  There is a “widget” plugin type! •  Each field widget is from that type!
  18. 18. Structure
  19. 19. SelectWidget.php namespace DrupaloptionsPluginFieldFieldWidget;
  20. 20. SelectWidget.php namespace DrupaloptionsPluginFieldFieldWidget;
  21. 21. SelectWidget.php namespace DrupaloptionsPluginFieldFieldWidget;
  22. 22. SelectWidget.php namespace DrupaloptionsPluginFieldFieldWidget;
  23. 23. Namespaces •  Namespaces were introduced in PHP 5.3 •  Used to avoid name collisions
  24. 24. PSR-0 •  Aims to provide a standard file, class and namespace convention to allow plug-and-play code. •  Uses the defined namespaces to automatically load files when needed. •  One on one mapping between namespaces and directory structure.
  25. 25. SelectWidget.php namespace DrupaloptionsPluginFieldFieldWidget;
  26. 26. Structure
  27. 27. PSR-4 •  “New standard” •  Makes alternative mapping possible •  Drupal will use it to remove “unneeded” folders. The empty *drupal* and *module-name* folder won’t be needed anymore.
  28. 28. Let’s recap •  We know how to find plugins •  We understand namespaces and the directory structure •  We understand PSR-0 and the difference between PSR-4
  29. 29. SelectWidget.php
  30. 30. Available methods
  31. 31. Car module structure
  32. 32. car.info.yml
  33. 33. Step 1: done
  34. 34. Let’s recap •  We know how to start with writing a field widget plugin •  We know how to start writing a custom module
  35. 35. Step 2 It should be possible to plug in different cars into the list.
  36. 36. Steven’s plan •  Create a custom plugin type named “Car” •  Create two sample car plugins
  37. 37. Custom plugin type •  Create a plugin manager service record •  Create an annotation class •  Create the plugin manager class
  38. 38. Custom plugin type
  39. 39. car.services.yml
  40. 40. Car module structure (part 2)
  41. 41. Sample plugins
  42. 42. Sample plugins
  43. 43. getOptions() v2
  44. 44. Debug output
  45. 45. Car module structure (part 3)
  46. 46. Let’s recap •  We know how to write a basic plugin manager •  We know how to handle annotations •  We know how to get all the plugin definitions
  47. 47. Disadvantage Each car requires a plugin
  48. 48. Derivative •  Allows you to create multiple plugin definitions through a single plugin class.
  49. 49. Derivative example! •  Create a yml file containing cars! •  Create a derivative plugin! •  Create a definition for each car in the yml file!
  50. 50. cars-example.yml
  51. 51. YamlCar.php
  52. 52. Step 2: done
  53. 53. Let’s recap •  We know when to use derivatives •  We are able to build one
  54. 54. Step 3 The name and an image of the selected car should placed on the homepage.
  55. 55. Steven’s plan •  Add a function to the plugins that returns an image url •  Create the frontpage
  56. 56. hook_menu is gone!
  57. 57. car.routing.yml
  58. 58. Car module structure (part 4)
  59. 59. Let’s recap •  We figured out how to add a function to a plugin •  We know how to create a plugin instance •  We saw how we should create our routes •  We learned how to build a page controller
  60. 60. The end
  61. 61. Questions?
  62. 62. THANK YOU!

×