- Issue created by @Axael
- πΊπΈUnited States john.oltman
Thanks for the post @Axael
You are correct, there is not an action yet. However, this is similar to what the Registration Confirmation submodule does, just need to send to the host entity owner instead of the registrant. If you can work a custom module into your use case, I'll post some code for you. Let me know.
- π§πͺBelgium Axael
Thanks for your answer @john.oltman !
Yes indeed, we could have a custom module worked out in order to add that feature.
If you have any tips, code, advice, please feel free to let me know :) - πΊπΈUnited States john.oltman
Put this in mymodule/my_module.services.yml:
services: my_module.event_subscriber: class: \Drupal\my_module\EventSubscriber\RegistrationEventSubscriber arguments: [ '@plugin.manager.action', '@my_module.logger', ] tags: - { name: event_subscriber } my_module.logger: parent: logger.channel_base arguments: [ 'my_module', ]
Put this in mymodule/src/EventSubscriber/RegistrationEventSubscriber.php:
<?php namespace Drupal\my_module\EventSubscriber; use Drupal\Core\Action\ActionManager; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\registration\Event\RegistrationEvent; use Drupal\registration\Event\RegistrationEvents; use Drupal\user\EntityOwnerInterface; use Psr\Log\LoggerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** * Provides a registration event subscriber. */ class RegistrationEventSubscriber implements EventSubscriberInterface { use StringTranslationTrait; /** * The action plugin manager. * * @var \Drupal\Core\Action\ActionManager */ protected ActionManager $actionManager; /** * The logger. * * @var \Psr\Log\LoggerInterface */ protected LoggerInterface $logger; /** * Constructs a new RegistrationEventSubscriber. * * @param \Drupal\Core\Action\ActionManager $action_manager * The action manager. * @param \Psr\Log\LoggerInterface $logger * The logger. */ public function __construct(ActionManager $action_manager, LoggerInterface $logger) { $this->actionManager = $action_manager; $this->logger = $logger; } /** * Process update. * * @param \Drupal\registration\Event\RegistrationEvent $event * The registration event. */ public function onUpdate(RegistrationEvent $event) { $registration = $event->getRegistration(); $from_state = isset($registration->original) ? $registration->original->getState()->id() : NULL; $to_state = $registration->getState()->id(); // Send a confirmation email to the route author for newly completed registrations. if (($from_state !== $to_state) && $registration->isComplete()) { if ($host_entity = $registration->getHostEntity()) { if ($entity = $host_entity->getEntity()) { if ($entity instanceof EntityOwnerInterface) { $configuration['recipient'] = $entity->getOwner()->getEmail(); $configuration['subject'] = $this->t('Someone registered for route @title', [ '@title' => $entity->label(), ]); $configuration['message'] = [ 'value' => $this->t('Example email body for confirmation email to route author'), 'format' => 'plain_text', // change this as needed ]; $configuration['mail_tag'] = 'my_module'; $configuration['log_message'] = FALSE; $action = $this->actionManager->createInstance('registration_send_email_action'); $action->setConfiguration($configuration); if ($action->execute($registration)) { $this->logger->info('Sent route author confirmation email to %recipient', [ '%recipient' => $configuration['recipient'], ]); } } } } } } /** * {@inheritdoc} */ public static function getSubscribedEvents(): array { return [ RegistrationEvents::REGISTRATION_INSERT => 'onUpdate', RegistrationEvents::REGISTRATION_UPDATE => 'onUpdate', ]; } }
Add an info file if this is a new custom module, and you should be set. Good luck!
- Status changed to Closed: works as designed
8 months ago 12:43pm 21 April 2024 Apologies for commenting on a closed issue, but I wanted to add that I could also use this functionality (and will be implementing the custom module solution you provided, thanks!) should you wish to explore adding it in future.
- π§πͺBelgium Axael
Hey John, sorry I haven't been able to test your code before today, some other unexpected projects were push in my workflow.
I've had the chance to test the code you posted and I can't get it to work. I might have done something wrong.I created a custom module with the code above, enabled it on my Drupal instance and tried registering to an event. The user gets the confirmation email, but the owner of the event doesn't get the notification email. The dblog doesn't display anything related to that new module neither except for the installation notice.
- πΊπΈUnited States john.oltman
@Axael is the registration complete? The code I posted only sends the email for newly completed registration, not all new registrations.
- π§πͺBelgium Axael
@john.oltman thanks for your answer. Yes, the registration is complete (that's my default status) and I tried multiple times with newly created registrations on newly created events.
I don't even think the code is fired as the logger doesn't display anything.
- πΊπΈUnited States john.oltman
@Axael the problem is the sample code I posted assumes the registration is created in one state, and then transitions later to complete. Do you have other states or just complete? Based on your answer I can post a modified version of the sample code.
- π§πͺBelgium Axael
@john.oltman Ah, yes indeed, now I get it:
public function onUpdate ()
In my case I only have one state (complete) as there is no validation needed, it's for a private car-sharing platform.
I'd be very thankful if you could post an updated code, thank you :)
- πΊπΈUnited States john.oltman
<?php namespace Drupal\my_module\EventSubscriber; use Drupal\Core\Action\ActionManager; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\registration\Event\RegistrationEvent; use Drupal\registration\Event\RegistrationEvents; use Drupal\user\EntityOwnerInterface; use Psr\Log\LoggerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** * Provides a registration event subscriber. */ class RegistrationEventSubscriber implements EventSubscriberInterface { use StringTranslationTrait; /** * The action plugin manager. * * @var \Drupal\Core\Action\ActionManager */ protected ActionManager $actionManager; /** * The logger. * * @var \Psr\Log\LoggerInterface */ protected LoggerInterface $logger; /** * Constructs a new RegistrationEventSubscriber. * * @param \Drupal\Core\Action\ActionManager $action_manager * The action manager. * @param \Psr\Log\LoggerInterface $logger * The logger. */ public function __construct(ActionManager $action_manager, LoggerInterface $logger) { $this->actionManager = $action_manager; $this->logger = $logger; } /** * Process insert. * * @param \Drupal\registration\Event\RegistrationEvent $event * The registration event. */ public function onInsert(RegistrationEvent $event) { $registration = $event->getRegistration(); // Send a confirmation email to the route author for newly completed registrations. if ($registration->isComplete()) { if ($host_entity = $registration->getHostEntity()) { if ($entity = $host_entity->getEntity()) { if ($entity instanceof EntityOwnerInterface) { $configuration['recipient'] = $entity->getOwner()->getEmail(); $configuration['subject'] = $this->t('Someone registered for route @title', [ '@title' => $entity->label(), ]); $configuration['message'] = [ 'value' => $this->t('Example email body for confirmation email to route author'), 'format' => 'plain_text', // change this as needed ]; $configuration['mail_tag'] = 'my_module'; $configuration['log_message'] = FALSE; $action = $this->actionManager->createInstance('registration_send_email_action'); $action->setConfiguration($configuration); if ($action->execute($registration)) { $this->logger->info('Sent route author confirmation email to %recipient', [ '%recipient' => $configuration['recipient'], ]); } } } } } } /** * {@inheritdoc} */ public static function getSubscribedEvents(): array { return [ RegistrationEvents::REGISTRATION_INSERT => 'onInsert', ]; } }
- πΊπΈUnited States john.oltman
@Axael give the code in my previous comment a try - it only emails when a registration is added, and does not need to check state since you only have one.
- π§πͺBelgium Axael
@john.oltman thanks, this works like a charm.
Thank you very, very much. This is much appreciated!