# 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.

{% code title="view/adminhtml/ui\_component/magehook\_event\_options\_example\_event.xml" %}

```markup
<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>
```

{% endcode %}

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.

{% code title="etc/webhooks.xml" %}

```markup
<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>
```

{% endcode %}

```php
<?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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://magehook.gitbook.io/magehook/event-options.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
