webhooks-8.x-1.x-dev/src/Hook/WebhooksHooks.php
src/Hook/WebhooksHooks.php
<?php
declare(strict_types=1);
namespace Drupal\webhooks\Hook;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
use Drupal\webhooks\Webhook;
/**
* Hook implementations for webhooks module.
*/
class WebhooksHooks {
/**
* Implements hook_help().
*/
#[Hook('help')]
public function help($route_name, RouteMatchInterface $route_match): string|\Stringable|array|null {
switch ($route_name) {
case 'help.page.webhooks':
$output = '';
$output .= '<h2>' . t('About') . '</h2>';
$output .= '<p>' . t('Webhooks are "user-defined HTTP callbacks". They are usually triggered by some event, such as pushing code to a repository or a comment being posted to a blog. When that event occurs, the source site makes an HTTP request to the URI configured for the Webhook. – <a href=":citation">https://en.wikipedia.org/wiki/Webhook</a>.', [
':citation' => 'https://en.wikipedia.org/wiki/Webhook',
]) . '</p>';
$output .= '<p>' . t('This module acts as both a Webhook dispatcher and a Webhook receiver. It is possible to map attributes of the Webhook payload to Webhook entity fields for storage.') . '</p>';
$output .= '<h2>' . t('Uses') . '</h2>';
$output .= '<dl>';
$output .= '<dt><h3>' . t('Dispatching') . '</h3></dt>';
$output .= '<dd>' . t('For dispatching you can <a href=":configure">configure any amount of Webhooks</a> that act on various events like entity create, update, delete and some system hooks like cron, file_download, modules_installed, user_cancel, user_login, user_logout and cache_flush.', [
':configure' => Url::fromRoute('entity.webhook_config.collection')->toString(),
]) . '</dd>';
$output .= '<dt><h3>' . t('Receiving') . '</h3></dt>';
$output .= '<dd>' . t('For receiving you can <a href=":configure">configure any amount of Webhooks</a>, the module captures the request and provides the payload to others with a generic Webhook event.', [
':configure' => Url::fromRoute('entity.webhook_config.collection')->toString(),
]) . '</dd>';
$output .= '<dt><h3>' . t('Storing') . '</h3></dt>';
$output .= '<dd>' . t('The Webhook module implements an entity type that stores every Webhook that has been received. Received Webhooks are stored in a <a href=":configure">dedicated Webhook entity type</a>, if the the type identifier and the Webhook machine name matches. The Webhook entity type is fieldable and you may <a href=":map">map Webhook payload attributes</a> to be stored in the configured fields.', [
':configure' => Url::fromRoute('entity.webhook_type.collection')->toString(),
':map' => Url::fromRoute('webhook.webhook_type.field_mapping', ['webhook_type' => 'webhook'])->toString(),
]) . '</dd>';
$output .= '<dt><h3>' . t('Acting on events') . '</h3></dt>';
$output .= '<dd>' . t('Since Webhooks trigger events and Webhook is an entity with fields that store the data, you can use modules like <a href=":rules">Rules</a> and <a href=":eca">ECA: Event - Condition - Action</a> to react when receiving, sending or storing Webhooks.', [
':rules' => 'https://www.drupal.org/project/rules',
':eca' => 'https://www.drupal.org/project/eca',
]) . '</dd>';
$output .= '</dl>';
$output .= '<p>' . t('Implemented as configuration entity Webhooks are easily deployable.') . '</p>';
return $output;
}
return NULL;
}
/**
* Implements hook_entity_insert().
*/
#[Hook('entity_insert')]
public function entityInsert(EntityInterface $entity): void {
/** @var \Drupal\webhooks\WebhooksService $webhooks_service */
$webhooks_service = \Drupal::service('webhooks.service');
/** @var \Symfony\Component\Serializer\Serializer $serializer */
$serializer = \Drupal::service('serializer');
$user = User::load(\Drupal::currentUser()->id());
$events = [
implode(':', ['entity', $entity->getEntityType()->id(), 'create']),
implode(':', ['entity', $entity->getEntityType()->id(), $entity->bundle(), 'create']),
];
foreach ($events as $event) {
$webhook_configs = $webhooks_service->loadMultipleByEvent($event);
/** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
foreach ($webhook_configs as $webhook_config) {
$webhook = new Webhook(
[
'event' => $event,
'user' => $serializer->normalize($user),
'entity' => $serializer->normalize($entity),
],
[],
$event,
$webhook_config->getContentType()
);
$webhooks_service->send($webhook_config, $webhook);
}
}
}
/**
* Implements hook_entity_update().
*/
#[Hook('entity_update')]
public function entityUpdate(EntityInterface $entity): void {
/** @var \Drupal\webhooks\WebhooksService $webhooks_service */
$webhooks_service = \Drupal::service('webhooks.service');
/** @var \Symfony\Component\Serializer\Serializer $serializer */
$serializer = \Drupal::service('serializer');
$user = User::load(\Drupal::currentUser()->id());
$events = [
implode(':', ['entity', $entity->getEntityType()->id(), 'update']),
implode(':', ['entity', $entity->getEntityType()->id(), $entity->bundle(), 'update']),
];
foreach ($events as $event) {
$webhook_configs = $webhooks_service->loadMultipleByEvent($event);
/** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
foreach ($webhook_configs as $webhook_config) {
$webhook = new Webhook(
[
'event' => $event,
'user' => $serializer->normalize($user),
'entity' => $serializer->normalize($entity),
],
[],
$event,
$webhook_config->getContentType()
);
$webhooks_service->send($webhook_config, $webhook);
}
}
}
/**
* Implements hook_entity_delete().
*/
#[Hook('entity_delete')]
public function entityDelete(EntityInterface $entity): void {
/** @var \Drupal\webhooks\WebhooksService $webhooks_service */
$webhooks_service = \Drupal::service('webhooks.service');
/** @var \Symfony\Component\Serializer\Serializer $serializer */
$serializer = \Drupal::service('serializer');
$user = User::load(\Drupal::currentUser()->id());
$events = [
implode(':', ['entity', $entity->getEntityType()->id(), 'delete']),
implode(':', ['entity', $entity->getEntityType()->id(), $entity->bundle(), 'delete']),
];
foreach ($events as $event) {
$webhook_configs = $webhooks_service->loadMultipleByEvent($event);
/** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
foreach ($webhook_configs as $webhook_config) {
$webhook = new Webhook(
[
'event' => $event,
'user' => $serializer->normalize($user),
'entity' => $serializer->normalize($entity),
],
[],
$event,
$webhook_config->getContentType()
);
$webhooks_service->send($webhook_config, $webhook);
}
}
}
/**
* Implements hook_cron().
*/
#[Hook('cron')]
public function cron(): void {
/** @var \Drupal\webhooks\WebhooksService $webhooks_service */
$webhooks_service = \Drupal::service('webhooks.service');
$event = implode(':', ['system', 'cron']);
$webhook_configs = $webhooks_service->loadMultipleByEvent($event);
/** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
foreach ($webhook_configs as $webhook_config) {
$webhook = new Webhook(
[
'event' => $event,
],
[],
$event,
$webhook_config->getContentType()
);
$webhooks_service->send($webhook_config, $webhook);
}
}
/**
* Implements hook_file_download().
*/
#[Hook('file_download')]
public function fileDownload($uri): array|int|null {
/** @var \Drupal\webhooks\WebhooksService $webhooks_service */
$webhooks_service = \Drupal::service('webhooks.service');
/** @var \Symfony\Component\Serializer\Serializer $serializer */
$serializer = \Drupal::service('serializer');
$event = implode(':', ['system', 'file_download']);
$webhook_configs = $webhooks_service->loadMultipleByEvent($event);
/** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
foreach ($webhook_configs as $webhook_config) {
$webhook = new Webhook(
[
'event' => $event,
'uri' => $serializer->normalize($uri),
],
[],
$event,
$webhook_config->getContentType()
);
$webhooks_service->send($webhook_config, $webhook);
}
return NULL;
}
/**
* Implements hook_modules_installed().
*/
#[Hook('modules_installed')]
public function modulesInstalled($modules, $is_syncing): void {
/** @var \Drupal\webhooks\WebhooksService $webhooks_service */
$webhooks_service = \Drupal::service('webhooks.service');
/** @var \Symfony\Component\Serializer\Serializer $serializer */
$serializer = \Drupal::service('serializer');
$event = implode(':', ['system', 'modules_installed']);
$webhook_configs = $webhooks_service->loadMultipleByEvent($event);
/** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
foreach ($webhook_configs as $webhook_config) {
$webhook = new Webhook(
[
'event' => $event,
'modules' => $serializer->normalize($modules),
],
[],
$event,
$webhook_config->getContentType()
);
$webhooks_service->send($webhook_config, $webhook);
}
}
/**
* Implements hook_user_cancel().
*/
#[Hook('user_cancel')]
public function userCancel($edit, UserInterface $account, $method): void {
/** @var \Drupal\webhooks\WebhooksService $webhooks_service */
$webhooks_service = \Drupal::service('webhooks.service');
/** @var \Symfony\Component\Serializer\Serializer $serializer */
$serializer = \Drupal::service('serializer');
$event = implode(':', ['system', 'user_cancel']);
$user = User::load($account->id());
$webhook_configs = $webhooks_service->loadMultipleByEvent($event);
/** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
foreach ($webhook_configs as $webhook_config) {
$webhook = new Webhook(
[
'event' => $event,
'user' => $serializer->normalize($user),
],
[],
$event,
$webhook_config->getContentType()
);
$webhooks_service->send($webhook_config, $webhook);
}
}
/**
* Implements hook_user_login().
*/
#[Hook('user_login')]
public function userLogin(UserInterface $account): void {
/** @var \Drupal\webhooks\WebhooksService $webhooks_service */
$webhooks_service = \Drupal::service('webhooks.service');
/** @var \Symfony\Component\Serializer\Serializer $serializer */
$serializer = \Drupal::service('serializer');
$event = implode(':', ['system', 'user_login']);
$user = User::load($account->id());
$webhook_configs = $webhooks_service->loadMultipleByEvent($event);
/** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
foreach ($webhook_configs as $webhook_config) {
$webhook = new Webhook(
[
'event' => $event,
'user' => $serializer->normalize($user),
],
[],
$event,
$webhook_config->getContentType()
);
$webhooks_service->send($webhook_config, $webhook);
}
}
/**
* Implements hook_user_logout().
*/
#[Hook('user_logout')]
public function userLogout(AccountInterface $account): void {
/** @var \Drupal\webhooks\WebhooksService $webhooks_service */
$webhooks_service = \Drupal::service('webhooks.service');
/** @var \Symfony\Component\Serializer\Serializer $serializer */
$serializer = \Drupal::service('serializer');
$event = implode(':', ['system', 'user_logout']);
$user = User::load($account->id());
$webhook_configs = $webhooks_service->loadMultipleByEvent($event);
/** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
foreach ($webhook_configs as $webhook_config) {
$webhook = new Webhook(
[
'event' => $event,
'user' => $serializer->normalize($user),
],
[],
$event,
$webhook_config->getContentType()
);
$webhooks_service->send($webhook_config, $webhook);
}
}
/**
* Implements hook_cache_flush().
*/
#[Hook('cache_flush')]
public function cacheFlush() {
/** @var \Drupal\webhooks\WebhooksService $webhooks_service */
$webhooks_service = \Drupal::service('webhooks.service');
$event = implode(':', ['system', 'cache_flush']);
$webhook_configs = $webhooks_service->loadMultipleByEvent($event);
/** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
foreach ($webhook_configs as $webhook_config) {
$webhook = new Webhook(
[
'event' => $event,
],
[],
$event,
$webhook_config->getContentType()
);
$webhooks_service->send($webhook_config, $webhook);
}
}
}
