3

So I'm trying to get an event subscriber working for EntityTypeEvents and I can't figure out why it's not firing.

This is a general outline of the code:

namespace Drupal\subscriber_class_module;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Entity\EntityTypeEvents;
use Drupal\Core\Entity\EntityTypeEvent;

class SubscriberClassModuleSubscribesToEntityTypeEvents implements EventSubscriberInterface {

    public function onCreate(EntityTypeEvent $event)  {
          //Do things on creation
    }
    public function onUpdate(EntityTypeEvent $event)  {
          //Do things on update
    }
    public function onDelete(EntityTypeEvent $event)  {
          //Do things on delete
    }
    static function getSubscribedEvents() {
         $events = [];

         $events[EntityTypeEvents::CREATE][] = array('onCreate', 100);
         $events[EntityTypeEvents::UPDATE][] = array('onUpdate', 100);
         $events[EntityTypeEvents::DELETE][] = array('onDelete', 100);
         return $events;
    }
}

Added class declaration and namespacing. I should note that the same class that I am using has no probablem firing on kernel events, I'm just wondering if there is something special about entity events.

3
  • Please show your class declaration and namespace as well.
    – Jaypan
    Commented Feb 2, 2017 at 6:20
  • Any news on this? I do have the same problem..
    – Moema
    Commented Jul 12, 2018 at 10:17
  • @Moema I moved on using hooks to create the events and then used an event subscriber. Unfortunately it seems these events can't be subscribed to or they are an oddity and the usual method doesn't work for them. I never figured out why so I just built it myself.
    – mcoatney
    Commented Aug 11, 2018 at 2:50

1 Answer 1

3

i already have the same issue, you have to implement this class to EntityTypeListenerInterface and use the trait EntityTypeEventSubscriberTrait

class SubscriberClassModuleSubscribesToEntityTypeEvents implements EntityTypeListenerInterface, EventSubscriberInterface{

use EntityTypeEventSubscriberTrait;

public function __construct(EntityManagerInterface $entity_manager) {
    $this->entityManager = $entity_manager;
}

public function onCreate(EntityTypeEvent $event)  {
      //Do things on creation
}
public function onUpdate(EntityTypeEvent $event)  {
      //Do things on update
}
public function onDelete(EntityTypeEvent $event)  {
      //Do things on delete
}

/**
 * {@inheritdoc}
 */
public static function getSubscribedEvents() {
    return static::getEntityTypeEvents();
}

}

and you get to load entity manager in your module.service.yml declaration arguments: ['@entity.manager']

1
  • Thanks! I went with building my own event but thank you for answering this, I'm sure I'll run into this again at some point.
    – mcoatney
    Commented Mar 26, 2019 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.