rng-3.x-dev/src/RegistrationStorage.php
src/RegistrationStorage.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <?php namespace Drupal\rng; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\Sql\SqlContentEntityStorage; use Drupal\rng\Event\RegistrationEvent; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * Defines the registration storage. */ class RegistrationStorage extends SqlContentEntityStorage { /** * The event dispatcher. * * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface */ protected $eventDispatcher ; /** * {@inheritdoc} */ public static function createInstance(ContainerInterface $container , EntityTypeInterface $entity_type ) { $instance = parent::createInstance( $container , $entity_type ); $instance ->setDispatcher( $container ->get( 'event_dispatcher' )); return $instance ; } /** * Sets the event dispatcher. * * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher * The event dispatcher. */ public function setDispatcher(EventDispatcherInterface $event_dispatcher ) { $this ->eventDispatcher = $event_dispatcher ; } /** * {@inheritdoc} */ protected function invokeHook( $hook , EntityInterface $entity ) { parent::invokeHook( $hook , $entity ); $this ->eventDispatcher->dispatch( new RegistrationEvent( $entity ), $this ->getEventName( $hook )); } /** * Gets the event name for the given hook. * * Created using the the entity type's module name and ID. * For example, the 'presave' hook for registration entities maps * to the 'rng.registration.presave' event name. * * @param string $hook * One of 'load', 'create', 'presave', 'insert', 'update', 'predelete', * 'delete', 'translation_insert', 'translation_delete'. * * @return string * The event name. */ protected function getEventName( $hook ) { return $this ->entityType->getProvider() . '.' . $this ->entityType->id() . '.' . $hook ; } } |