Slide 1

Slide 1 text

Drupal8 for Symfony Developers Antonio Perić-Mažar
 01.07.2017 @ #dpc

Slide 2

Slide 2 text

@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

Slide 3

Slide 3 text

@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

Slide 4

Slide 4 text

@antonioperic Fun Facts

Slide 5

Slide 5 text

@antonioperic

Slide 6

Slide 6 text

@antonioperic

Slide 7

Slide 7 text

@antonioperic 31 years = 978 264 705 seconds

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

@antonioperic Drupal 101 for SF developers

Slide 10

Slide 10 text

@antonioperic Before we start, small disclaimer

Slide 11

Slide 11 text

@antonioperic My world is Symfony

Slide 12

Slide 12 text

@antonioperic —

Slide 13

Slide 13 text

@antonioperic

Slide 14

Slide 14 text

@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

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

@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”

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

@antonioperic

Slide 19

Slide 19 text

@antonioperic

Slide 20

Slide 20 text

@antonioperic

Slide 21

Slide 21 text

@antonioperic

Slide 22

Slide 22 text

@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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

@antonioperic Development environment

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

@antonioperic Tools

Slide 30

Slide 30 text

@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

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

@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

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

drupal list

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

@antonioperic Installation

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

@antonioperic Let’s use composer

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

@antonioperic Run in browser

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

@antonioperic Add some content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

drupal create:nodes

Slide 61

Slide 61 text

drupal create:nodes

Slide 62

Slide 62 text

@antonioperic We have content but something is missing

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

@antonioperic WebProfiler

Slide 65

Slide 65 text

@antonioperic Module/Devel

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

drush pm-enable devel

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

@antonioperic Under the Hood

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

@antonioperic Request -> Response

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

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

Slide 89

Slide 89 text

Deeper in Drupal End of Request

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

@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

Slide 92

Slide 92 text

@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

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

@antonioperic

Slide 97

Slide 97 text

@antonioperic

Slide 98

Slide 98 text

@antonioperic

Slide 99

Slide 99 text

@antonioperic

Slide 100

Slide 100 text

@antonioperic Routing

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

@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}).

Slide 103

Slide 103 text

@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

Slide 104

Slide 104 text

@antonioperic Controllers

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

@antonioperic Services

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

@antonioperic Events / Listeners

Slide 115

Slide 115 text

@antonioperic

Slide 116

Slide 116 text

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

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

No content

Slide 119

Slide 119 text

@antonioperic Building Custom Module
 hello_symfony

Slide 120

Slide 120 text

@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

Slide 121

Slide 121 text

@antonioperic hello_symfony.info.yml

Slide 122

Slide 122 text

No content

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

@antonioperic

Slide 125

Slide 125 text

@antonioperic

Slide 126

Slide 126 text

@antonioperic hello_symfony.routing.yml

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

@antonioperic

Slide 129

Slide 129 text

@antonioperic

Slide 130

Slide 130 text

@antonioperic hello_symfony.links.menu.yml

Slide 131

Slide 131 text

No content

Slide 132

Slide 132 text

@antonioperic

Slide 133

Slide 133 text

@antonioperic PageExampleController

Slide 134

Slide 134 text

No content

Slide 135

Slide 135 text

@antonioperic

Slide 136

Slide 136 text

No content

Slide 137

Slide 137 text

@antonioperic

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

@antonioperic

Slide 140

Slide 140 text

No content

Slide 141

Slide 141 text

@antonioperic

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

@antonioperic

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

No content

Slide 146

Slide 146 text

No content

Slide 147

Slide 147 text

@antonioperic

Slide 148

Slide 148 text

No content

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

@antonioperic

Slide 151

Slide 151 text

@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

Slide 152

Slide 152 text

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