0

I tried to trigger event on each entity update. This is my src/EventSubscriber/MySub.php

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

    class MySub implements EventSubscriberInterface{

      public function onEntityTypeUpdate(EntityTypeEvents $event)  {
            drupal_set_message("returned from onEntityTypeUpdate"); 
       }

      public static function getSubscribedEvents() {

          $events[EntityTypeEvents::UPDATE][] = array('onEntityTypeUpdate');


    return $events;
  }
}

This is my services.yml file

services:
  custom_sub:
    class: '\Drupal\custom_sub\EventSubscriber\MySub'
    arguments: ['@entity.manager']
    tags:
      - { name: 'event_subscriber' }

But the message is not displaying after updating the content or content type. Please guide.

  • You define arguments but don't use them in your class. Try to delete arguments: ['@entity.manager'] and clear the cache. – Jdrupal Sep 11 at 10:06
  • i did. I could get event triggered while defining a new custom event and using hook_node_insert but not in EntityTypeEvents::UPDATE. Can anyone tell me why? – Max28 Sep 11 at 10:51
  • Both are different events. The hook is triggered when a node is inserted. The event is triggered when the entity type definition is updated, which normally only happens when you install a module which alters the entity type definition of the node entity. – 4k4 Sep 11 at 11:29
  • which means adding field to existing content type would trigger the UPDATE event? – Max28 Sep 11 at 12:02
  • Not necessarily, only if you add a field storage to the entity type at the same time it would change the database schema of the entity type, but I'm not sure it triggers on changes in the configured fields. – 4k4 Sep 11 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.