This repository has been archived on 2025-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
oliverdavies.uk-old-sculpin/source/_posts/2018-08-21-experimenting-with-events-in-drupal-8.md

70 lines
2.4 KiB
Markdown
Raw Normal View History

2018-08-21 12:32:58 +00:00
---
title: Experimenting with events in Drupal 8
2018-12-31 12:13:05 +00:00
excerpt: Trying a different way of structuring Drupal modules, using event subscribers and autowiring.
2018-08-21 12:32:58 +00:00
tags:
- drupal
- drupal-8
2018-08-23 06:46:24 +00:00
- drupal-planet
2018-08-21 12:32:58 +00:00
- php
2018-08-23 06:46:24 +00:00
- symfony
2019-01-15 08:26:11 +00:00
favourite: true
2018-08-21 12:32:58 +00:00
---
2018-12-31 12:13:05 +00:00
Ive been experimenting with moving some code to Drupal 8, and Im quite intrigued by a different way that Ive tried to structure it - using event subscribers, building on some of the takeaways from Drupal Dev Days.
2018-08-21 12:32:58 +00:00
Here is how this module is currently structured:
2018-09-04 18:58:54 +00:00
![](/images/blog/events-drupal-8/1.png){.border .p-1}
2018-08-21 12:32:58 +00:00
Note that there is no `opdavies_blog.module` file, and rather than calling actions from within a hook like `opdavies_blog_entity_update()`, each action becomes its own event subscriber class.
This means that there are no long `hook_entity_update` functions, and instead there are descriptive, readable event subscriber class names, simpler action code that is responsibile only for performing one task, and youre able to inject and autowire dependencies into the event subscriber classes as services - making it easier and cleaner to use dependency injection, and simpler write tests to mock dependencies when needed.
2018-12-31 12:13:05 +00:00
The additional events are provided by the [Hook Event Dispatcher module](https://www.drupal.org/project/hook_event_dispatcher).
2018-08-21 12:32:58 +00:00
## Code
`opdavies_blog.services.yml`:
```yaml
services:
2018-08-22 06:31:56 +00:00
Drupal\opdavies_blog\EventSubscriber\PostToMedium:
2018-08-21 12:32:58 +00:00
autowire: true
tags:
- { name: event_subscriber }
Drupal\opdavies_blog\EventSubscriber\SendTweet:
autowire: true
tags:
- { name: event_subscriber }
```
2018-09-03 06:46:04 +00:00
<div class="note" markdown="1">
Adding `autowire: true` is not required for the event subscriber to work. Im using it to automatically inject any dependencies into the class rather than specifying them separately as arguments.
</div>
2018-08-21 12:32:58 +00:00
`src/EventSubscriber/SendTweet.php`:
```php
namespace Drupal\opdavies_blog\EventSubscriber;
use Drupal\hook_event_dispatcher\Event\Entity\EntityUpdateEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SendTweet implements EventSubscriberInterface {
...
public static function getSubscribedEvents() {
return [
HookEventDispatcherInterface::ENTITY_UPDATE => 'sendTweet',
];
}
public function sendTweet(EntityUpdateEvent $event) {
// Perform checks and send the tweet.
}
}
```