Drupal at your service! By Mauro Vigliotti About me Mauro - - PowerPoint PPT Presentation

drupal at your service
SMART_READER_LITE
LIVE PREVIEW

Drupal at your service! By Mauro Vigliotti About me Mauro - - PowerPoint PPT Presentation

Drupal at your service! By Mauro Vigliotti About me Mauro Vigliotti Im from Argentina (South America) Drupal developer since 2005 Working for The Brick Factory Developer by day rocker by night 2018


slide-1
SLIDE 1

Drupal… at your service!

By Mauro Vigliotti

slide-2
SLIDE 2

About me

Mauro Vigliotti

  • I’m from Argentina (South America)
  • Drupal developer since 2005
  • Working for The Brick Factory
  • Developer by day…
  • … rocker by night
  • 2018 DrupalCon Official guitar winner!

2

slide-3
SLIDE 3

About The Brick Factory

The Brick Factory is a Washington, DC-based digital agency that plans and executes digital campaigns for nonprofits, trade associations, advocacy groups and brands

  • Digital Strategy
  • Digital Marketing
  • Design
  • Development
  • Hosting and Systems
  • Products

3

slide-4
SLIDE 4

Get ready to learn about one of Drupal 8’s greatest new features!

4

slide-5
SLIDE 5

Overview

What’s a Service The Service Container Service Definition Tags Accessing a Service Samples:

  • Page redirection
  • Controlling user sessions
  • Overriding an existing service
  • Adding service using DI

5

slide-6
SLIDE 6

What will you get from this talk?

A good approach to D8 Services Sample code to start producing your own services, it’s so easy! Understanding about Service Container, controller classes and the new Symfony approach on Drupal Core

6

slide-7
SLIDE 7

This talk is not intended to:

Go deeply into Symfony Provide *all* the services that are available within the Drupal core Solve *all* your Drupal 8 problems Talk about REST (in Drupal 7, Services’ meaning is different)

7

slide-8
SLIDE 8

Important note! Why do I say this talk is targeted to beginners?

8

slide-9
SLIDE 9

Target: Drupal < 8 developers

The learning curve for most of the developers we’ve been working on previous Drupal versions before can be hard, mostly because the CMS had a hook-based approach, and now is moving to an Object-Oriented Paradigm.

9

slide-10
SLIDE 10

Target: Drupal freshmen.

If you’re brand new on Drupal, finding out there’s so many things to learn could be the first blocker, whether if you’re a content manager, a designer, a front-end or back-end developer.

10

slide-11
SLIDE 11

Let’s go!

11

slide-12
SLIDE 12

What’s a Drupal 8 Service?

Let’s throw the mystery away, a Service is a PHP Class (or a bunch of classes, depending on your implementation).

  • Decoupled reusable functionality
  • Plugged to your Drupal 8 at any time
  • Introduced in Drupal by Symfony.

12

slide-13
SLIDE 13

What’s a Drupal 8 Service?

13

slide-14
SLIDE 14

Drupal Services Out-of-the-box

  • More than 100 services available.
  • Sample services:

○ Accessing the current logged in user ○ Sending an email ○ Asking for a page redirection ○ Altering a http response ○ Performing a database query ○ Translating a piece of text

  • Full core services list here:

https://api.drupal.org/api/drupal/core%21core.services.yml/8.2.x ...or opening the core/core.services.yml file.

14

slide-15
SLIDE 15

The Service Container (a.k.a. Dependency Injection Container)

Dependency Injection Container

Service A Service B Service C Service X Service C.01 Service C

15

slide-16
SLIDE 16

Service definition

YAML file in root of our custom module

16

slide-17
SLIDE 17

Service definition

Service definition format

https://www.drupal.org/docs/8/api/services-and-dependency-injection/structure-of-a-service-file

user.current_user_context: class: Drupal\user\ContextProvider\CurrentUserContext arguments: ['@current_user', '@entity.manager'] tags:

  • { name: 'context_provider' }

Service name Designated class Arguments Tags

17

slide-18
SLIDE 18

Tags

  • Define a group of related services

(remember the card deck concept)

  • Tag properties

○ name ○ priority ○ global ○ alias, and many others

  • Some sample tag names:

○ access_check ○ cache.bin ○ context_provider ○ event_subscriber ○ theme_negotiator https://www.drupal.org/docs/8/api/services-and-dependency-injection/structure-of-a-service-file

18

slide-19
SLIDE 19

Accessing a Service

19

slide-20
SLIDE 20

Accessing a Service: YAML Service file

user.current_user_context: class: Drupal\user\ContextProvider\CurrentUserContext arguments: ['@current_user', '@entity.manager'] tags:

  • { name: 'context_provider' }

20

slide-21
SLIDE 21

Accessing a Service: $container object

// EntityViewBuilder.php public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { return new static( $entity_type, $container->get('entity.manager'), $container->get('language_manager'), $container->get('theme.registry') ); }

21

slide-22
SLIDE 22

Accessing a Service everywhere: Global \Drupal class

$foobar = \Drupal::service('foo.bar');

22

slide-23
SLIDE 23

Okay… enough talking… Show me the code!

23

slide-24
SLIDE 24

Sample Code

  • Page redirection
  • Controlling user sessions
  • Overriding an existing service
  • Adding service using DI

24

slide-25
SLIDE 25

Questions?

25

slide-26
SLIDE 26

Thank you!

Get in touch if you have any questions

  • Drupal.org: https://www.drupal.org/u/mauro_ptt

Presentation and code ready to download at:

  • Presentation: https://goo.gl/Vr8DTK
  • Sample code: https://github.com/maurotbf/d8services

26

slide-27
SLIDE 27

2018 The Brick Factory

27