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.

Antonio Peric-Mazar

July 01, 2017
Tweet

More Decks by Antonio Peric-Mazar

Other Decks in Programming

Transcript

  1. Drupal8 for Symfony
    Developers
    Antonio Perić-Mažar

    01.07.2017 @ #dpc

    View Slide

  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
    [email protected]
    • @antonioperic

    View Slide

  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

    View Slide

  4. @antonioperic
    Fun Facts

    View Slide

  5. @antonioperic

    View Slide

  6. @antonioperic

    View Slide

  7. @antonioperic
    31 years =
    978 264 705 seconds

    View Slide

  8. @antonioperic
    Questions?
    • Any Drupal experts/developers here?
    • Symfony developers?
    • Symfony developers without Drupal knowledge

    View Slide

  9. @antonioperic
    Drupal 101 for SF
    developers

    View Slide

  10. @antonioperic
    Before we start, small
    disclaimer

    View Slide

  11. @antonioperic
    My world is Symfony

    View Slide

  12. @antonioperic

    View Slide

  13. @antonioperic

    View Slide

  14. @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
    • More learning for Drupal developers (OOP, Symfony, new concepts)
    • Connecting two big communities

    View Slide

  15. View Slide

  16. @antonioperic
    Who is it for?
    • Content strategist
    • Site Administrators
    • Content editors
    • “Build stuff without writing code”
    • v8.3.2
    • Professional developers
    • Bespoke applications
    • “Make writing code easier”

    View Slide

  17. @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.

    View Slide

  18. @antonioperic

    View Slide

  19. @antonioperic

    View Slide

  20. @antonioperic

    View Slide

  21. @antonioperic

    View Slide

  22. @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

    View Slide

  23. @antonioperic
    Symfony Components
    • ClassLoader
    • Console
    • CssSelector
    • DependencyInjection
    • EventDispatcher
    • HttpFoundation
    • HttpKernel
    • Process
    • Routing
    • Serializer
    • Translation
    • Validator
    • Yaml

    View Slide

  24. @antonioperic
    • HTTP Kernel
    • Request / Response
    • Controllers
    • Event Dispatching
    • Listeners / Subscribers
    Dependency injection container

    View Slide

  25. @antonioperic
    Development environment

    View Slide

  26. View Slide

  27. View Slide

  28. @antonioperic
    Or just use your Symfony
    development environment
    • PHP built-in server
    • Vagrant
    • Docker
    • …

    View Slide

  29. @antonioperic
    Tools

    View Slide

  30. @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

    View Slide

  31. View Slide

  32. View Slide

  33. View Slide

  34. View Slide

  35. View Slide

  36. @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

    View Slide

  37. View Slide

  38. View Slide

  39. drupal list

    View Slide

  40. View Slide

  41. View Slide

  42. @antonioperic
    Installation

    View Slide

  43. @antonioperic
    How to install Drupal8
    • Drush
    • drush dl drupal
    • Composer
    • Download zip file

    View Slide

  44. @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.

    View Slide

  45. @antonioperic
    Let’s use composer

    View Slide

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

    View Slide

  47. View Slide

  48. View Slide

  49. @antonioperic
    Let’s use composer
    composer require drupal/
    for example:
    composer require drupal/token

    View Slide

  50. @antonioperic
    Let’s use composer
    composer require drupal/
    for example:
    composer require drupal/token
    drupal module:install token

    View Slide

  51. @antonioperic
    Run in browser

    View Slide

  52. View Slide

  53. View Slide

  54. View Slide

  55. View Slide

  56. View Slide

  57. View Slide

  58. @antonioperic
    Add some content

    View Slide

  59. View Slide

  60. drupal create:nodes

    View Slide

  61. drupal create:nodes

    View Slide

  62. @antonioperic
    We have content but
    something is missing

    View Slide

  63. View Slide

  64. @antonioperic
    WebProfiler

    View Slide

  65. @antonioperic
    Module/Devel

    View Slide

  66. @antonioperic
    composer require drupal/devel
    drupal module:install devel

    View Slide

  67. View Slide

  68. View Slide

  69. View Slide

  70. drush pm-enable devel

    View Slide

  71. View Slide

  72. View Slide

  73. View Slide

  74. View Slide

  75. View Slide

  76. View Slide

  77. View Slide

  78. @antonioperic
    Under the Hood

    View Slide

  79. View Slide

  80. View Slide

  81. View Slide

  82. View Slide

  83. View Slide

  84. View Slide

  85. @antonioperic
    Request -> Response

    View Slide

  86. View Slide

  87. @antonioperic
    What happens when request
    enters Drupal
    1. Bootstrap configuration:
    ◦ Read the settings.php file, generate some other settings dynamically, and store
    them both in global variables and the Drupal\Component\Utility\Settings singleton
    object.
    ◦ Start the class loader, that takes care of loading classes.
    ◦ Set the Drupal error handler.
    ◦ Detect if Drupal is actually installed. If it is not, redirect to the installer script.
    2. Create the Drupal kernel.
    3. Initialize the service container (either from cache or from rebuild).
    4. Add the container to the Drupal static class.
    5. Attempt to serve page from static page cache (just like Drupal 7).
    6. Load all variables (variable_get).

    View Slide

  88. @antonioperic
    What happens when request
    enters Drupal
    7. Load other necessary (procedural) include files.
    8. Register stream wrappers (public://, private://, temp:// and custom
    wrappers).
    9. Create the HTTP Request object (using the Symfony HttpFoundation
    component).
    10. Let the DrupalKernel handle it and return a response.
    11. Send the response.
    12. Terminate the request (modules can act upon this event).

    View Slide

  89. Deeper in Drupal
    End of Request

    View Slide

  90. View Slide

  91. @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

    View Slide

  92. @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

    View Slide

  93. View Slide

  94. View Slide

  95. View Slide

  96. @antonioperic

    View Slide

  97. @antonioperic

    View Slide

  98. @antonioperic

    View Slide

  99. @antonioperic

    View Slide

  100. @antonioperic
    Routing

    View Slide

  101. View Slide

  102. @antonioperic
    Available “defaults” keys
    • _controller

    The specified method is simply called with the specified route parameters, and is
    expected to return a response.
    • _content

    If specified, the _controller is set based on the request's mime type, and fills the
    content of the response with the result of the specified method (usually a string or
    render array).
    • _form

    If specified, the _controller is set to HtmlFormController::content, which responds with
    the specified form. This form must be a fully qualified class name (or service id) that
    implements FormInterface and usually extends FormBase. Indeed, form building has
    also become object oriented!
    • _entity_form

    If specified, the _controller is set to HtmlEntityFormController::content, which
    responds with the specified entity form (specified as {entity_type}.{add|edit|delete}).

    View Slide

  103. @antonioperic
    Available “requirements” keys
    • _permission

    The current user must have the specified permission.
    • _role

    The current user must have the specified role.
    • _method

    The allowed HTTP methods (GET, POST, etc).
    • _scheme

    Set to https or http. The request scheme must be the same as the specified scheme. This
    property is also taken into account when generating urls (Drupal::url(..)) rather than routing.
    If set, urls will have this scheme set fixed.
    • _node_add_access

    A custom access check for adding new nodes of some node type.
    • _entity_access

    A generic access checker for entities.
    • _format

    Mime type formats

    View Slide

  104. @antonioperic
    Controllers

    View Slide

  105. View Slide

  106. View Slide

  107. View Slide

  108. View Slide

  109. View Slide

  110. View Slide

  111. @antonioperic
    Services

    View Slide

  112. View Slide

  113. View Slide

  114. @antonioperic
    Events / Listeners

    View Slide

  115. @antonioperic

    View Slide

  116. @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:

    View Slide

  117. View Slide

  118. View Slide

  119. @antonioperic
    Building Custom Module

    hello_symfony

    View Slide

  120. @antonioperic
    Creating custom model
    Steps:
    • Register your module (module_name.info.yml)
    • Add routing (*.routing.yml)
    • Add controller
    • Clear cache
    • Test if page is rendering well

    View Slide

  121. @antonioperic
    hello_symfony.info.yml

    View Slide

  122. View Slide

  123. View Slide

  124. @antonioperic

    View Slide

  125. @antonioperic

    View Slide

  126. @antonioperic
    hello_symfony.routing.yml

    View Slide

  127. View Slide

  128. @antonioperic

    View Slide

  129. @antonioperic

    View Slide

  130. @antonioperic
    hello_symfony.links.menu.yml

    View Slide

  131. View Slide

  132. @antonioperic

    View Slide

  133. @antonioperic
    PageExampleController

    View Slide

  134. View Slide

  135. @antonioperic

    View Slide

  136. View Slide

  137. @antonioperic

    View Slide

  138. View Slide

  139. @antonioperic

    View Slide

  140. View Slide

  141. @antonioperic

    View Slide

  142. View Slide

  143. @antonioperic

    View Slide

  144. View Slide

  145. View Slide

  146. View Slide

  147. @antonioperic

    View Slide

  148. View Slide

  149. View Slide

  150. @antonioperic

    View Slide

  151. @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
    • Documentation is not the best, read examples and code

    View Slide

  152. @antonioperic
    Thank you!
    [email protected]
    @antonioperic
    www.locastic.com
    Please rate my talk
    https://joind.in/talk/a434b

    View Slide