1

I have a EventSubscriber which I want to trigger before the entity is saved (to alter the title). But I can't figure why it doesn't work. I got back the projet, do the migration from D8 to D9. Is it possible that is came from the D9 migration ?

I could simply add the hook_entity_presave in a module, but I guess it's cleaner to add that in an EventSubscriber

  my_module.entity_event_subscriber:
    class: Drupal\my_module\EventSubscriber\EntityEventSubscriber
    arguments: ['@entity_type.manager','@entity_field.manager']
    tags:
      - { name: event_subscriber }

Here my event

<?php

namespace Drupal\my_module\EventSubscriber;

use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\hook_event_dispatcher\Event\Entity\EntityPresaveEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class EntityEventSubscriber implements EventSubscriberInterface {

  private $em;
  private $entityFieldManager;

  public function __construct(EntityTypeManagerInterface $em, EntityFieldManagerInterface $entityFieldManager) {
    $this->em = $em;
    $this->entityFieldManager = $entityFieldManager;
  }
  public static function getSubscribedEvents() {
    return [
      HookEventDispatcherInterface::ENTITY_PRE_SAVE => 'entitySave',
    ];
  }

  /**
   *
   */
  public function entitySave(EntityPresaveEvent $event) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $event->getEntity();
    $entity_type_id = $entity->getEntityTypeId();
    $bundle = $entity->bundle();
    if (in_array($entity_type_id, ['node', 'node_custom'])) {
        /// logic
    }
  }
}
6
  • 2
    "I could simply add the hook_entity_presave in a module, but I guess it's cleaner to add that in an EventSubscriber" - No, as long as core provides this API in hooks and you need an extra module to dispatch the event (via the same hook by the way), this is definitely not a cleaner solution.
    – 4uk4
    Commented Jun 25, 2021 at 9:41
  • Isn't the EventSubscriber native to Drupal ?
    – user104792
    Commented Jun 25, 2021 at 9:55
  • 2
    The hook_event_dispatcher isn't native... Commented Jun 25, 2021 at 10:26
  • Oh yeah, it's a (n old) module ! Didn't notice that.... So how to I use the presave in the subscriber ?
    – user104792
    Commented Jun 25, 2021 at 11:11
  • Use hook_entity_presave.
    – Kevin
    Commented Jun 25, 2021 at 13:02

1 Answer 1

1

Hook Event Dispatcher in version 8.x-2.x have had their EntityPresaveEvent namespace switched from hook_event_dispatcher to core_event_dispatcher in Move Entity events to core_event_dispatcher.

Please change these references accordingly:

Before:

use Drupal\hook_event_dispatcher\Event\Entity\EntityPresaveEvent;

After:

use Drupal\core_event_dispatcher\Event\Entity\EntityPresaveEvent;

Also ensure you install the new core_event_dispatcher dependency.

See also ExampleEntityEventSubscribers.php for example usages.

Your Answer

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