实体事件

事件和事件订阅者。


事件

名称描述
EntityEvents::ENTITY_ACTION当实体被插入、更新或删除时。
EntityEvents::ENTITY_REVALIDATED当实体被重新验证时

事件订阅者

实体插入、更新或删除

当实体被插入、更新或删除时,创建一个事件订阅者:EntityEvents::ENTITY_ACTION

custom_module/src/EventSubscriber/EntityActionEventSubscriber.php

<?php
namespace Drupal\custom_module\EventSubscriber;
use Drupal\next\Event\EntityActionEvent;
use Drupal\next\Event\EntityEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Defines an event subscriber for entity action events.
*/
class EntityActionEventSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[EntityEvents::ENTITY_ACTION] = ['onAction'];
return $events;
}
/**
* Responds to the action event.
*
* @param \Drupal\next\Event\EntityActionEvent $event
* The event.
*/
public function onAction(EntityActionEvent $event) {
// Get the action: insert, updated or delete.
$action = $event->getAction();
}
}

重新验证

当实体被重新验证时(**重新验证后**)创建一个事件订阅者:EntityEvents::ENTITY_REVALIDATED

custom_module/src/EventSubscriber/EntityRevalidatedEventSubscriber.php

<?php
namespace Drupal\next_tests\EventSubscriber;
use Drupal\next\Event\EntityEvents;
use Drupal\next\Event\EntityRevalidatedEventInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Defines an event subscriber for entity revalidated events.
*/
class EntityRevalidatedEventSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[EntityEvents::ENTITY_REVALIDATED] = ['onRevalidated'];
return $events;
}
/**
* Responds to entity revalidated.
*
* @param \Drupal\next\Event\EntityRevalidatedEventInterface $event
* The event.
*/
public function onRevalidated(EntityRevalidatedEventInterface $event) {
if ($event->isRevalidated()) {
// Do something if entity has been successfully revalidated.
}
}
}