Commit 7d6cb348 authored by Michael Stenta's avatar Michael Stenta
Browse files

Issue #3306227: Dispatch events for asset presave, insert, update, delete

parent 29106edd
Loading
Loading
Loading
Loading
+50 −0
Changes for modules/core/asset/asset.module: 50 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@
 * Hooks and customizations for the asset module.
 */

use Drupal\asset\Entity\AssetInterface;
use Drupal\asset\Event\AssetEvent;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;

@@ -24,6 +26,54 @@ function asset_help($route_name, RouteMatchInterface $route_match) {
  return $output;
}

/**
 * Implements hook_ENTITY_TYPE_presave().
 */
function asset_asset_presave(AssetInterface $asset) {

  // Dispatch an event on asset presave.
  // @todo Replace this with core event via https://www.drupal.org/node/2551893.
  $event = new AssetEvent($asset);
  $event_dispatcher = \Drupal::service('event_dispatcher');
  $event_dispatcher->dispatch($event, AssetEvent::PRESAVE);
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function asset_asset_insert(AssetInterface $asset) {

  // Dispatch an event on asset insert.
  // @todo Replace this with core event via https://www.drupal.org/node/2551893.
  $event = new AssetEvent($asset);
  $event_dispatcher = \Drupal::service('event_dispatcher');
  $event_dispatcher->dispatch($event, AssetEvent::INSERT);
}

/**
 * Implements hook_ENTITY_TYPE_update().
 */
function asset_asset_update(AssetInterface $asset) {

  // Dispatch an event on asset update.
  // @todo Replace this with core event via https://www.drupal.org/node/2551893.
  $event = new AssetEvent($asset);
  $event_dispatcher = \Drupal::service('event_dispatcher');
  $event_dispatcher->dispatch($event, AssetEvent::UPDATE);
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function asset_asset_delete(AssetInterface $asset) {

  // Dispatch an event on asset delete.
  // @todo Replace this with core event via https://www.drupal.org/node/2551893.
  $event = new AssetEvent($asset);
  $event_dispatcher = \Drupal::service('event_dispatcher');
  $event_dispatcher->dispatch($event, AssetEvent::DELETE);
}

/**
 * Implements hook_theme().
 */
+35 −0
Changes for modules/core/asset/src/Event/AssetEvent.php: 35 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\asset\Event;

use Drupal\Component\EventDispatcher\Event;
use Drupal\asset\Entity\AssetInterface;

/**
 * Event that is fired by asset save, delete and clone operations.
 */
class AssetEvent extends Event {

  const PRESAVE = 'asset_presave';
  const INSERT = 'asset_insert';
  const UPDATE = 'asset_update';
  const DELETE = 'asset_delete';

  /**
   * The Asset entity.
   *
   * @var \Drupal\asset\Entity\AssetInterface
   */
  public AssetInterface $asset;

  /**
   * Constructs the object.
   *
   * @param \Drupal\asset\Entity\AssetInterface $asset
   *   The Asset entity.
   */
  public function __construct(AssetInterface $asset) {
    $this->asset = $asset;
  }

}