Basics

Rules of engagement

MageHook is build with extensibility in mind. This will keep one of the focus points through time. The MageHook_Hook module serves as an extension base layer. PR's on new events will not be accepted. In order to build your own custom event you need to create a sub-module on top of the MageHook_Hook extension. The MageHook_Hook module will only provide you the required tools in order to dispatch events in a manageable way.

Sub module examples

As an example the Magento_Catalog will get extended with Magento_CatalogHookEvents for it's hook events. Please keep in mind in order to keep uniformity, it is important that you use the 'Hooks' affix.

  • Vendor_HookEventsCustomers

  • Vendor_HookEventsCatalog

  • Vendor_HookEventsCheckout

  • Vendor_HookEventsAdmin

  • Vendor_HookEventsIndexer

Type examples

Please read further through the documentation for more information about Types.

  • Vendor_HookTypeAmazonAlexa

  • Vendor_HookTypePusher

  • Vendor_HookTypeFacebook

  • Vendor_HookTypeGoogleHome

  • Vendor_HookTypeDeployer

Basic example

etc/webhook.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:MageHook_Hook/etc/webhooks.xsd">
  <hook event="test_hook_event"
        title="Test Hook Title"
        purpose="Test Purpose"
        group="Test Group"
        service="MageHook\Hook\Service\TestInterface"/>
</config>

XML attributes

Title (options)

  • Title

  • Title (Purpose)

  • Group - Title

  • Group - Title (Purpose)

Service (optional)

A Service Contract is a set of PHP interfaces that are defined for a module . A service contract includes data interfaces, which preserve data integrity, and service interfaces, which hide business logic details from service requestors such as controllers, web services, and other modules. If developers define data and service interfaces according to a set of design patterns, the result is a well-defined, durable API that other modules and third-party extensions can implement through Magento models and resource models.

Click here for more information about Service Contracts

etc/webhooks.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:MageHook_Hook/etc/webhooks.xsd">
  <hook event="example_event"
        title="Example Title"
        purpose="Example Purpose"
        group="Example Group"
        service="Magento\Catalog\Api\Data\ProductInterface"/>
</config>

Converter (optional)

A converter can be used if data needs to be adjusted, checked or secured afterwards. The converter is injected after the optional Service Class. This makes it the final step before the given data is converted to a json body and forwarded to the Message Queue.

etc/webhooks.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:MageHook_Hook/etc/webhooks.xsd">
  <hook event="example_event"
        title="Example Title"
        purpose="Example Purpose"
        group="Example Group"
        service="Magento\Catalog\Api\Data\ProductInterface"
        converter="Basic\Example\Model\Converter\Product"/>
</config>

List (optional)

The list attribute gives a developer the option to hide the event from the adminhtml listing. By default the admin listing will use 'adminhtml' to search for events.

etc/webhooks.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:MageHook_Hook/etc/webhooks.xsd">
  <hook event="example_event"
        title="Example Title"
        purpose="Example Purpose"
        group="Example Group"
        service="Magento\Catalog\Api\Data\ProductInterface"
        converter="Basic\Example\Model\Converter\Product"
        list="frontend"/>
</config>

In the example above you can get a custom list of events specially defined for frontend users (a.k.a Customers or Guests).

In order to get a custom list, use the getList() method within the \MageHook\Hook\Helper\Events Helper class.

Dispatch example

<?php

declare(strict_types=1);

namespace Basic\Example\Plugin\Magento\Newsletter\Model;

use MageHook\Hook\ManagerInterface as HookManagerInterface;
use Magento\Newsletter\Model\Subscriber as Subject;

/**
 * Class Subscriber
 *
 * @package Basic\Example\Plugin\Magento\Newsletter\Model
 */
class Subscriber
{
    /** @var HookManagerInterface $hookEventsManagerInterface */
    protected $hookEventsManagerInterface;

    /**
     * OrderRepositoryInterface constructor.
     * @param HookManagerInterface $hookEventsManagerInterface
     */
    public function __construct(
        HookManagerInterface $hookEventsManagerInterface
    ) {
        $this->hookEventsManagerInterface = $hookEventsManagerInterface;
    }

    /**
     * @param Subject $subject
     * @return Subject
     */
    public function afterSave(Subject $subject): Subject
    {
        if ($subject->isStatusChanged()) {
            $this->hookEventsManagerInterface->fire('newsletter_subscription_change', $subject->getData());
        }

        return $subject;
    }
}

Last updated