Event Options

Event options give you the ability to combine multiple and/or kinda similar events into a single event.

Based on additional UI components, a develop gets the possibility to add extra fields to a "event container" which is basically just a fieldset container who listens/links to the selected event.

Basic idea

In previous versions of MageHook you would have to create a event for both login and logout. Now you can add one single event with an added multiselect field which provides you the option to select if the hook needs to be dispatched both on login and logout, or one of both.

A developer has multiple options, like the "additional" array parameter, to mark the authentication method (login or logout). This method can be reflected within a custom ValidationInterface who has the power to accept or cancel the final dispatchment.

Milestones

  • Implement a look a like Sales Rule Conditions configurator

  • Option to extend existing validator classes (based on a pool) without having to use a before/after plugin

Add new event options field(s)

As a (event) developer, you have the ability to create your own custom event options UI component. These components will be shown within the backend (edit-) form under the "Event Options" tab. The only thing you have to do is adding a UI component XML to view/adminhtml/ui_component. The filename requires a 'magehook_event_options' prefix extended with the event namespace.

view/adminhtml/ui_component/magehook_event_options_example_event.xml
<container name="example_event"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
  <field name="name">
    <argument name="data" xsi:type="array">
      <item name="config" xsi:type="array">
        <item name="dataType" xsi:type="string">text</item>
        <item name="label" xsi:type="string" translate="true">Validation Field</item>
        <item name="formElement" xsi:type="string">input</item>
        <item name="sortOrder" xsi:type="number">10</item>
        <item name="dataScope" xsi:type="string">validation_field</item>
        <item name="validation" xsi:type="array">
          <item name="required-entry" xsi:type="boolean">true</item>
        </item>
      </item>
    </argument>
  </field>
</container>

You don't have to worry about any save actions. This is been taken care of where only the selected option values for the particular event will be stored as the "custom_options".

Validation

Custom event options will only work if you provide the event with a ValidationInterface who can be defined within the "validator" element.

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"
        group="Example Group"
        validator="Basic\Example\Event\Options\ExampleValidator"/>
</config>
<?php

declare(strict_types=1);

namespace Basic\Example\Event\Options;

use MageHook\Hook\Api\Data\WebhookInterface;
use MageHook\Hook\Event\Options\ValidatorInterface;
use Magento\Framework\Event\Observer as FrameworkObserver;

/**
 * Class ExampleValidator
 *
 * @package Basic\Example\Event\Options
 */
class ExampleValidator implements ValidatorInterface
{
    /**
     * @return bool
     */
    public function validate(): bool
    {
        return true;
    }
    
    /**
     * @return bool
     */
    public function validateDataVariable(): bool
    {
        return true;
    }
    
    /**
     * @return bool
     */
    public function hasKey(string $key, array $data): bool
    {
        return isset($data[$key]);
    }
}

Validations can be split up into multiple smaller pieces to get clean and easy to read code. All methods starting with the 'validate' key will be automatically processed on which they always return a boolean. As long as the result is true, the methods will be processed from the top down. All remaining methods will be skipped when one of the methods doesn't return true.

Extend existing Event- Options & Validators

All event options UI components can be extended by default. In order to validate those options, you need to write a before/after plugin on the existing validator class.

Last updated