You are here

entity_events.module in Entity Events 2.0.x

Same filename and directory in other branches
  1. 8 entity_events.module

This module holds functions for entity events.

File

entity_events.module
View source
<?php

/**
 * @file
 * This module holds functions for entity events.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\entity_events\EntityEventType;
use Drupal\entity_events\Event\EntityEvent;
use Drupal\Component\EventDispatcher\Event;

/**
 * Implements hook_entity_insert().
 *
 * {@inheritdoc}
 */
function entity_events_entity_insert(EntityInterface $entity) {
  $event_type = EntityEventType::INSERT;
  $event = new EntityEvent($event_type, $entity);
  _entity_events_dispatch($event_type, $event);
}

/**
 * Implements hook_entity_update().
 *
 * { @inheritdoc }
 */
function entity_events_entity_update(EntityInterface $entity) {
  $event_type = EntityEventType::UPDATE;
  $event = new EntityEvent($event_type, $entity);
  _entity_events_dispatch($event_type, $event);
}

/**
 * Implements hook_entity_presave().
 *
 * { @inheritdoc }
 */
function entity_events_entity_presave(EntityInterface $entity) {
  $event_type = EntityEventType::PRESAVE;
  $event = new EntityEvent($event_type, $entity);
  _entity_events_dispatch($event_type, $event);
}

/**
 * Implements hook_entity_delete().
 *
 * {@inheritdoc}
 */
function entity_events_entity_delete(EntityInterface $entity) {
  $event_type = EntityEventType::DELETE;
  $event = new EntityEvent($event_type, $entity);
  _entity_events_dispatch($event_type, $event);
}

/**
 * Helper method to dispatch an event.
 *
 * @param string $event_type
 *   The event type.
 * @param \Drupal\Component\EventDispatcher\Event $event
 *   The event to dispatch.
 */
function _entity_events_dispatch($event_type, Event $event) {

  /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */
  $dispatcher = \Drupal::service('event_dispatcher');
  $dispatcher
    ->dispatch($event, $event_type);
}