Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Drupal8 for Symfony Developers

Drupal8 for Symfony Developers

Drupal8 modernization (new object-oriented base) and adoption of many Symfony components is a huge step in connecting these two amazing communities and amazing projects. Drupal8 is not powered by full-stack Symfony and there is still many differences between these two relatives, but still, Symfony developers should master it easily.

This talk is for Symfony developers who don't have experience with Drupal8. It will guide you through routing, controllers, hooks, events, Drupal Console, DI and many other interesting elements that power Drupal8 under the hood. It will also show how to build custom modules Symfony way.

29db221e8a59b06c9180725ec8ac1e75?s=128

Antonio Peric-Mazar

February 21, 2017
Tweet

More Decks by Antonio Peric-Mazar

Other Decks in Programming

Transcript

  1. Drupal8 for Symfony Developers Antonio Perić-Mažar
 17.02.2017 @ #phpuk

  2. @antonioperic About me • Antonio Perić-Mažar, mag. ing. comp. •

    CEO, Co-Founder @ Locastic • Co-Founder @ Shift Conference • Software developer, Symfony2 • Open Source Contributor • SFUGCRO
 • www.locastic.com • antonio@locastic.com • @antonioperic
  3. @antonioperic Locastic • We help clients create amazing web and

    mobile apps (since 2011) • design and development agency • mobile development • web development • UX/UI • Training and Consulting • Shift Conference, Symfony Croatia • www.locastic.com • @locastic
  4. @antonioperic Inside Locastic

  5. @antonioperic Almost 25% of our team are women

  6. @antonioperic Questions? • Any Drupal experts/developers here? • Symfony developers?

    • Symfony developers without Drupal knowledge
  7. @antonioperic Drupal 101 for SF developers

  8. @antonioperic Before we start, small disclaimer

  9. @antonioperic My world is Symfony (still)

  10. @antonioperic —

  11. @antonioperic

  12. @antonioperic What this means • Drupal8 doesn’t use full stack

    Symfony, it uses components (maybe in future) • Moving Drupal to modern stack • Building powerful CMS on top of Symfony components • Connecting two big communities • More learning for Drupal developers (OOP, Symfony, new concepts)
  13. None
  14. @antonioperic Who is it for? • Content strategist • Site

    Administrators • Content editors • “Build stuff without writing code” • Professional developers • Bespoke applications • “Make writing code easier”
  15. @antonioperic In Drupal 8 there's three different types of knowledge

    that you're going to be using in order to work with it effectively.
  16. @antonioperic

  17. @antonioperic

  18. @antonioperic

  19. @antonioperic

  20. @antonioperic Symfony2 Framework Bundle Symfony2 Bundles Symfony2 CMF Bundles Symfony2

    Components Partnered Libs (twig, etc.) CMF Components Symfony fullstack Drupal 8 Distribution Drupal Core Modules Drupal Contrib Modules Drupal Core Libraries Symfony2 Components Partnered Libs (twig, etc.) Drupal Components
  21. @antonioperic Symfony Components • ClassLoader • Console • CssSelector •

    DependencyInjection • EventDispatcher • HttpFoundation • HttpKernel • Process • Routing • Serializer • Translation • Validator • Yaml
  22. @antonioperic • HTTP Kernel • Request / Response • Controllers

    • Event Dispatching • Listeners / Subscribers Dependency injection container
  23. @antonioperic Development environment

  24. None
  25. None
  26. @antonioperic Or just use your Symfony development environment • PHP

    built-in server • Vagrant • Docker • …
  27. @antonioperic Tools

  28. @antonioperic Drush • update core and contrib • download modules

    • enable modules • clear cache • update db • run cron • import config • export config • create user • change password • one time login • backup drupal • restore drupal • compile twig templates *Type “drush” to get full list - www.drushcommands.com
  29. None
  30. None
  31. None
  32. None
  33. None
  34. @antonioperic Drupal Console • update core and contrib • download

    modules • enable modules • clear cache • update db • run cron • import config • export config • generate console command • generate entity • generate content type • generate modules • run unit test *Type “drupal list” to get full list - drupalconsole.com/docs
  35. None
  36. None
  37. None
  38. None
  39. None
  40. @antonioperic Installation

  41. @antonioperic How to install Drupal8 • Drush • drush dl

    drupal • Composer • Download zip file
  42. @antonioperic Lets use composer • drupal/drupal. • This uses Drupal

    itself as a template for the new site. It is the simplest solution but lacks additional configuration that can be helpful.
 
 • drupal-composer/drupal-project. • This open source project acts as a kickstarter for Composer-based Drupal sites. It provides default configuration that otherwise needs to be added manually.
  43. @antonioperic Let’s use composer

  44. @antonioperic Let’s use composer composer create-project drupal/drupal

  45. None
  46. @antonioperic Let’s use composer composer require drupal/<modulename> for example: composer

    require drupal/token
  47. @antonioperic Run in browser

  48. None
  49. None
  50. None
  51. None
  52. None
  53. None
  54. @antonioperic Add some content

  55. None
  56. drupal create:nodes

  57. None
  58. @antonioperic We have content but something is missing

  59. None
  60. @antonioperic WebProfiler

  61. @antonioperic composer require drupal/devel

  62. None
  63. None
  64. None
  65. None
  66. None
  67. None
  68. None
  69. None
  70. None
  71. @antonioperic Under the Hood

  72. None
  73. None
  74. None
  75. None
  76. None
  77. None
  78. None
  79. @antonioperic Request -> Response

  80. None
  81. None
  82. @antonioperic Pipeline 1. After the controller returned a render array,

    the VIEW will be triggered by the HttpKernel, because the controller result is not a Response, but a render array. 2. MainContentViewSubscriber is subscribed to the VIEW event. It checks whether the controller result is an array, and if so, it guarantees to generate a Response. 3. Next, MainContentViewSubscriber checks whether the negotiated request format is supported: 1. Any format for which a main content renderer service exists (an implementation of MainContentRendererInterface is supported. 2. If the negotiated request format is not supported, a 406 JSON response is generated, which lists the supported formats in a machine-readable way (as per RFC 2616, section 10.4.7). 4. Otherwise, when the negotiated request format is supported, the corresponding main content renderer service is initialized. A response is generated by calling MainContentRendererInterface::renderResponse() on the service. That's it
  83. @antonioperic Main Content Renderes HTML: HtmlRenderer (text/html) AJAX: AjaxRenderer (application/vnd.drupal-ajax)

    Dialog: DialogRenderer (application/vnd.drupal-dialog) Modal: ModalRenderer (application/vnd.drupal-modal)
  84. None
  85. None
  86. @antonioperic Routing

  87. None
  88. @antonioperic Controllers

  89. None
  90. None
  91. None
  92. None
  93. None
  94. @antonioperic Services

  95. None
  96. None
  97. @antonioperic Events / Listeners

  98. @antonioperic

  99. @antonioperic Registering event subscribers Here are the steps to register

    an event subscriber: Define a service in your module, tagged with 'event_subscriber' (see the Services topic for instructions). Define a class for your subscriber service that implements \Symfony\Component\EventDispatcher\EventSubscriberInterface In your class, the getSubscribedEvents method returns a list of the events this class is subscribed to, and which methods on the class should be called for each one. Example: public static function getSubscribedEvents() { // Subscribe to kernel terminate with priority 100. $events[KernelEvents::TERMINATE][] = array('onTerminate', 100); // Subscribe to kernel request with default priority of 0. $events[KernelEvents::REQUEST][] = array('onRequest'); return $events; }
  100. @antonioperic Building Custom Module
 HelloPHPUK

  101. None
  102. None
  103. None
  104. None
  105. None
  106. None
  107. None
  108. None
  109. None
  110. None
  111. None
  112. None
  113. None
  114. None
  115. None
  116. None
  117. None
  118. None
  119. None
  120. None
  121. None
  122. None
  123. @antonioperic Recap • Symfony components are main building tool for

    D8 • DIC is backbone of Drupal • DI for controllers is very easy • Events are replacing hooks • Building custom module is easy • Hardest thing is to start • This is just beginning there is a lot to learn about and from D8
  124. @antonioperic Thank you! antonio@locastic.com @antonioperic www.locastic.com