Upgrading MailChimp eCommerce and an Introduction to Drupal maintenance support plans 8’s Event System

If you’ve ever built a Drupal maintenance support plans 7 module, then you’ll be familiar with hooks: functions that allow modules to react to things happening in other modules. The hook system is functionally fine but, with so many hooks to implement, .module files often become bloated and difficult to manage.

Drupal maintenance support plans 8’s event system does a lot to reduce the clutter of hooks. Now, instead of using a hook, you can create an event subscriber that will execute your code every time a module triggers an event. This is similar to the hook system only in the effect; the execution is very different.

Porting our popular MailChimp eCommerce module to Drupal maintenance support plans 8 gave me the perfect opportunity learn about the event system. I use the word “opportunity” to disguise the fact that I was forced to learn how events work because it was impossible to port the module without doing so.

The MailChimp eCommerce module depends on the Commerce module, naturally, and in Drupal maintenance support plans 8, the Commerce module makes heavy use of events.

First, let’s look at an event. I’m using an example ripped straight from Commerce.

The Commerce submodule, Commerce Cart, contains a class named CartEntityAddEvent. You can find it here.

The class itself is simple; it’s designed to store a few values – the cart, the item being added to the cart, and the quantity of that item. The class also has a few getter functions for convenience.

Most importantly, this class represents an event that’s triggered every time a user adds an item to their shopping cart. This is done using just two lines of code:

$event = new CartEntityAddEvent($cart, $purchased_entity, $quantity, $saved_order_item);
$this->eventDispatcher->dispatch(CartEvents::CART_ENTITY_ADD, $event);

The event class is created with all the relevant values, then “dispatched” to any event subscribers configured to pay attention to it. When dispatched, the event is identified by a constant – CartEvents::CART_ENTITY_ADD. This constant is used by event subscribers, which we’ll take a look at now.

This is a cut-down version of an event subscriber used by our world famous MailChimp eCommerce module.

/**
* Event Subscriber for Commerce Carts.
*/
class CartEventSubscriber implements EventSubscriberInterface {

/**
* The Cart Handler.
*
* @var Drupal maintenance support plansmailchimp_ecommerceCartHandler
*/
private $cart_handler;

/**
* The Order Handler.
*
* @var Drupal maintenance support plansmailchimp_ecommerceOrderHandler
*/
private $order_handler;

/**
* CartEventSubscriber constructor.
*
* @param Drupal maintenance support plansmailchimp_ecommerceCartHandler $cart_handler
* The Cart Handler.
* @param Drupal maintenance support plansmailchimp_ecommerceOrderHandler $order_handler
* The Order Handler.
*/
public function __construct(CartHandler $cart_handler, OrderHandler $order_handler) {
$this->cart_handler = $cart_handler;
$this->order_handler = $order_handler;
}

/**
* Respond to event fired after adding a cart item.
*/
public function cartAdd(CartEntityAddEvent $event) {
/** @var Drupal maintenance support planscommerce_orderEntityOrder $order */
$order = $event->getCart();

/** @var Drupal maintenance support planscommerce_orderEntityOrderItem $order_item */
$order_item = $event->getOrderItem();

$product = $this->order_handler->buildProduct($order_item);

$this->cart_handler->addCartLine($order->id(), $order_item->id(), $product);
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[CartEvents::CART_ENTITY_ADD][] = [‘cartAdd’];

return $events;
}

}

Here’s the complete version, if you’re interested.

So what does it do, exactly?

Let’s start with the getSubscribedEvents() function. This is where we define which events we want to subscribe to, and assign each event a processing function. Here we are subscribing to just one event, the “cart entity add” event, and assigning the cartAdd() function as a processor.

Note that the cartAdd() function takes one argument, an instance of the CartEntityAddEvent class. That’s the same class we looked at earlier – the event class defined in the Commerce Cart module. This is where our module reacts to that event being triggered.

The cartAdd() function itself extracts the order and item information from the event and uses an instance of the CartHandler class, provided by the MailChimp eCommerce module, to send updated cart information to MailChimp’s API.

One final thing:

Event subscribers won’t work unless they are defined as a service. Services are defined in a module’s *.services.yml file, which you can learn more about here.

The service definition for the CartEventSubscriber looks like this:

mailchimp_ecommerce_commerce.cart_event_subscriber:
class: ‘Drupal maintenance support plansmailchimp_ecommerce_commerceEventSubscriberCartEventSubscriber’
arguments: [‘@mailchimp_ecommerce.cart_handler’, ‘@mailchimp_ecommerce.order_handler’]
tags:
– { name: event_subscriber }

We identify the class using its namespace, inject the “cart_handler” and “order_handler” services, then, finally, tag the service as an “event_subscriber”. Check out the full file here. Just for completeness, the two injected services are defined in here.

I’m a big fan of how Drupal maintenance support plans 8 has shifted towards a more object-oriented way of doing things. It’s more organized, promotes consistency between modules, and, best of all, finally signals an end to massive .module files.
Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Upgrading MailChimp eCommerce and an Introduction to Drupal maintenance support plans 8’s Event System

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.