4. Wizzlern
‣ 100% Drupal
‣ Drupal training and consultancy
‣ Front-, backend-development
‣ Dev-ops and editorial
‣ Advanced subjects
‣ Since 2009
‣ Active in the Drupal community
4
10. Why services
‣ A solution to organise your code.
‣ Drupal 8 brings new possibilities, lets explore
them!
‣ For the beauty of the code.
‣ Service are unit-testable.
‣ Better code structure
= easier maintenance & scaling = cheaper.
10
16. Services
‣ Service = Software design pattern.
‣ Service = Provides a globally usable
functionality.
‣ Examples: Database, URL generator,
TranslationManager
‣ Advantages: Decoupled, Lazy loading.
16
17. Services
‣ Better structure, scaling and maintenance.
‣ Breaking code into smaller chunks makes it
more self-documenting.
‣ Let go of your procedural habits.
‣ Give it an interface!
17
18. Services
‣ Use a service for:
‣ Business logic from hooks
‣ Business logic from preprocess
‣ Business logic for custom content entities
‣ Helpers that require other service.
‣ Repeated or similar business logic from blocks,
controllers, plugins, etc.
‣ For independent logic
‣ API communication, Larger logic structures, etc.
18
19. As a developer I want to structure the code of the Recent
news block.
Exercise
‣ Are there parts in WeNewsRecentNews that can be
moved to a service?
‣ Create a service that contains business logic related
to the News content type.
‣ Details in: 01-recent-news/exercise.md
19
20. Service layers
‣ Grouping services into functional layers
reduces the impact of change.
‣ A service should contain only a specific type
of logic. Example: either reusable or process-
specific logic.
20
21. More structures
‣ Constants defined in an interface
‣ Decorator
‣ Trait
‣ A class that groups static helper methods
21
22. As a developer I want to structure the code of the News by
category group block.
Exercise
‣ What parts in WeNewsRecentNewsByGroup that can
be moved to a service?
‣ Create a service that contains business logic related
to the News Category vocabulary.
‣ Details in: 02-news-by-group/exercise.md
22
23. As a developer I want to structure code of the Related
news extra field.
Exercise
‣ Are there parts in the code that can be moved to a
service?
‣ Can we re-use an existing service or do we create a
new one?
‣ Details in: 03-related-news/exercise.md
23
24. As a developer I want to structure code of the news cron
task.
Exercise
‣ Are there parts in we_news_cron() that can be moved
to a service?
‣ Create a service that contains business logic related
to the news archive.
‣ Details in: 04-cron-service/exercise.md
24
26. Summary
‣ Structure for maintainability.
‣ Larger projects require more structure.
‣ Group related business logic.
‣ Services and interfaces are one of the tools.
Ask yourself what pattern matches best.
26
27. This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
http://wizzlern.nl
@wizzlern
27