Skip to content

Commit cd366c8

Browse files
committed
Document the new Event Filters feature
1 parent 9295f7c commit cd366c8

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

README.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ The Sender Class can be a subclass of the class that triggers the event. For exa
7272

7373
See [Integrating with Task Automation Tools](#integrating-with-task-automation-tools) for examples on how to get a Webhook URL from various task automation tools.
7474

75-
![Screenshot of the Edit Webhook page](./screenshot.png)
75+
![Screenshot of the Edit Webhook page](./images/edit-webhook.png)
7676

7777
Webhooks can either send a GET request, or a POST request with a JSON body containing the following keys:
7878

@@ -84,6 +84,54 @@ Webhooks can either send a GET request, or a POST request with a JSON body conta
8484
- `eventClass` – the class name of the event.
8585
- `event` – an object with keys representing any event class properties that aren’t declared by [yii\base\Event](https://www.yiiframework.com/doc/api/2.0/yii-base-event). (For example, if a [craft\events\ElementEvent](https://docs.craftcms.com/api/v3/craft-events-elementevent.html) is triggered, this will contain [element](https://docs.craftcms.com/api/v3/craft-events-elementevent.html#property-element) and [isNew](https://docs.craftcms.com/api/v3/craft-events-elementevent.html#property-isnew) keys.)
8686

87+
#### Filtering Events
88+
89+
Some events can have filters applied to them, which prevent webhooks from being executed under certain conditions.
90+
91+
![Screenshot of the Event Filters setting](./images/event-filters.png)
92+
93+
Only element class events and certain `craft\services\Elements` events have any filters out of the box, but modules and plugins can register additional filters using the `craft\webhooks\Plugin::EVENT_REGISTER_FILTER_TYPES` event.
94+
95+
```php
96+
use yii\base\Event;
97+
use craft\webhooks\Plugin as Webhooks;
98+
use craft\events\RegisterComponentTypesEvent;
99+
100+
Event::on(Webhooks::class, Webhooks::EVENT_REGISTER_FILTER_TYPES, function(RegisterComponentTypesEvent $e) {
101+
$e->types[] = ArticleFilter::class;
102+
});
103+
```
104+
105+
Filter type classes must implement `craft\webhooks\filters\FilterInterface`:
106+
107+
```php
108+
use craft\webhooks\filters\FilterInterface;
109+
use craft\elements\Entry;
110+
use yii\base\Event;
111+
112+
class ArticleFilter implements FilterInterface
113+
{
114+
public static function displayName(): string
115+
{
116+
return 'Entry has an “Article” type';
117+
}
118+
119+
public static function show(string $class, string $event): bool
120+
{
121+
// Only show this filter if the Sender Class is set to 'craft\elements\Entry'
122+
return $class === Entry::class;
123+
}
124+
125+
public static function check(Event $event, bool $value): bool
126+
{
127+
// Filter basen on whether the entry's type is 'article':
128+
/** @var Entry $entry */
129+
$entry = $event->sender;
130+
return ($entry->type->handle === 'article') === $value;
131+
}
132+
}
133+
```
134+
87135
#### Sending More Data
88136

89137
If you need more data than what’s in the default POST request payload, you can fill in the “Extra User Attributes”, “Extra Sender Attributes”, and “Extra Event Attributes” fields.
File renamed without changes.

images/event-filters.png

22 KB
Loading

0 commit comments

Comments
 (0)