- 982 views
This step is easy, just create your_module folder under /modules directory and place the following code inside the files:
- your_module.module (nothing much inside, only php opening, but the file needs to be present)
<?php
- your_module.info.yml
name: Your Module Name description: My new custom Event Subscriber for Drupal Commerce package: Grafeon type: module core: 8.x
In your module folder, create the file with a name your_module.services.yml, place this code inside:
services: your_module.order_complete: class: Drupal\grafeon_kredity\EventSubscriber\OrderCompleteSubscriber arguments: ['@entity_type.manager'] tags: - { name: event_subscriber }
What it does?
services:
- This is telling Drupal that we are about to work and inject our code into using services
your_module.order_complete:
- first part is the name of your module and second part is the event you are about to be listening to. In this case we are listening to Drupal Commerce Order Complete event. So when an user completes the order.
class: Drupal\grafeon_kredity\EventSubscriber\OrderCompleteSubscriber
- Tells where the code we are about to create sits in module directory. Folder structure is being set by default and Drupal is looking for EventSubscriber folders in a module's directories anyway.
arguments: ['@entity_type.manager']
- Arguments, that are being passed to an event
tags: - { name: event_subscriber }
- Those two lines are super necessary and do not leave them out. Tags are telling drupal that we are actually writing an Event Subscriber.
Drupal is looing for a structure in your module. It has to be like follows:
- Create /src folder in your module root directory
- Create EventSubscriber folder in /src directory, so now you have /src/EventSubscriber folder available
- Create OrderCompleteSubscriber.php file inside in EventSubscriber folder, so now you have /src/EventSubscriber/OrderCompleteSubscriber.php file available
In this tutorial, we are creating an Event Subscriber for Drupal Commerce, listening to Order Complete event. Place this code inside in your newly created OrderCompleteSubscriber.php file.
<?php namespace Drupal\your_module\EventSubscriber; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Drupal\state_machine\Event\WorkflowTransitionEvent; use Drupal\Core\Entity\EntityTypeManager; /** * Class OrderCompleteSubscriber. * * @package Drupal\your_module */ class OrderCompleteSubscriber implements EventSubscriberInterface { /** * Drupal\Core\Entity\EntityTypeManager definition. * * @var \Drupal\Core\Entity\EntityTypeManager */ protected $entityTypeManager; /** * Constructor. */ public function __construct(EntityTypeManager $entity_type_manager) { $this->entityTypeManager = $entity_type_manager; } /** * {@inheritdoc} */ static function getSubscribedEvents() { $events['commerce_order.place.post_transition'] = ['orderCompleteHandler']; return $events; } /** * This method is called whenever the commerce_order.place.post_transition event is * dispatched. * * @param WorkflowTransitionEvent $event */ public function orderCompleteHandler(WorkflowTransitionEvent $event) { /** @var \Drupal\commerce_order\Entity\OrderInterface $order */ $order = $event->getEntity(); // Order items in the cart. $items = $order->getItems(); // do something with the items in cart here if you wish... } }
Now it is up to you to work with the code. Of course you can listen to any event. Node add, node edit, user viewed and many more. Just like Rules does in Drupal 7, but with a lot more work. So hooray to coding!