You are here

function webhooks_entity_insert in Webhooks 8

Implements hook_entity_insert().

File

./webhooks.module, line 33
Contains webhooks.module.

Code

function webhooks_entity_insert(EntityInterface $entity) {

  /** @var \Drupal\webhooks\WebhooksService $webhooks_service */
  $webhooks_service = \Drupal::service('webhooks.service');

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');
  $event = implode(':', [
    'entity',
    $entity
      ->getEntityType()
      ->id(),
    'create',
  ]);
  $user = User::load(\Drupal::currentUser()
    ->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),
      'entity' => $serializer
        ->normalize($entity),
    ], [], $event, $webhook_config
      ->getContentType());
    $webhooks_service
      ->send($webhook_config, $webhook);
  }
}