0

I have used Scheduler module with content moderation integration to publish a content automatically. It is perfectly working using the module. Now I want to add a functionality that once the content is published via cron, an email will be sent to the author of that particular content.

I have used hook_scheduler_publish_action to perform this action, but unfortunately I am facing some different scenario.

  1. If I remove node publish code and keep only the mail sending code, the mail is fired.
  2. If I remove the mail sending code and keep only node publish code, the node is published.
  3. If I keep both, only mail is sent, content is not published.

Ref: https://git.drupalcode.org/project/scheduler/blob/8.x-1.x/scheduler.api.php

Below code is only publishing content, not sending email.

function MYMODULE_scheduler_publish_action(NodeInterface $node) {
  // node moderation state will be 'published'
  $node->set('moderation_state', 'published');
  $node->save();

  // email send to author
  $mail_params = array(
    'mail_subject' => 'run cron',
    'mail_body' => '<p>node published using scheduler</p>',
    'mail_to' => '[email protected]'
  );

  \Drupal::service('MYMODULE.common_service')->FunctionToSendEmail($mail_params);
}

Below code is only sending email, not publishing content.

function MYMODULE_scheduler_publish_action(NodeInterface $node) {  
  // email send to author
  $mail_params = array(
  'mail_subject' => 'run cron',
  'mail_body' => '<p>node published using scheduler</p>',
  'mail_to' => '[email protected]'
  );
  
  \Drupal::service('MYMODULE.common_service')->FunctionToSendEmail($mail_params);
  
  // node moderation state will be 'published'
  $node->set('moderation_state', 'published');
  $node->save();
}

That means this hook only perform the first action and ignores others.

Now my question is how will I perform multiple actions using this hook or is there any other procedure to do this?

Please help me. Thanks in advance.

1 Answer 1

0

The Scheduler Module itself do provide some events , which one of them is PUBLISH. So you can create an event subscriber in your module and listen to publish event like this:

class YourSchedulerEventSubscriber implements EventSubscriberInterface {
  public function publishAndSendMail(SchedulerEvent $event) {
  /** @var \Drupal\node\Entity\Node $node */
  $node = $event->getNode();
  $node->set('moderation_state', $node->publish_state->getValue());

  $event->setNode($node);

  $mail_params = array(
  'mail_subject' => 'run cron',
  'mail_body' => '<p>node published using scheduler</p>',
  'mail_to' => '[email protected]'
  );
  \Drupal::service('MYMODULE.common_service')->FunctionToSendEmail($mail_params); //Better to be sent as service argumnets
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    // The values in the arrays give the function names above.
    $events[SchedulerEvents::PUBLISH][] = ['publishAndSendMail'];
    return $events;
  }
}
6
  • Thank you for your kind help. I will definitely give it a try on Monday. Can you please provide me a link or something where I can get a full list of events like PUBLISH provided by this Scheduler module? Thanks again.
    – Souvik Das
    Commented Jan 1, 2022 at 19:37
  • Also, how will I send multiple parameters as array in the \Drupal::service method when the service parameters will be dynamic based on different node publishing scenario? The mail_to key will have different email ids that I need to send via the service. As per this ref, drupal.stackexchange.com/questions/237353/… services are not suitable in cases where I need to pass parameters from the service call.
    – Souvik Das
    Commented Jan 1, 2022 at 20:07
  • to answer your first question you can go to scheduler module in src/SchedulerEvents.php you can find the list of events Commented Jan 2, 2022 at 5:00
  • when you want to build a service and you need more services like MYMODULE.common_service as input you can add it as argument in your services.yml file (it needs an @ to be considered as service), then if you need more parameters which are not of type services, then in a case of eventsubscriber you need to fetch them from $event, for example to get a node author you can get it from $event->node->getoWNER() Commented Jan 2, 2022 at 5:08
  • Getting the same issue with your solution. Email sent but node not published. Once I remove the mail sending code, node is published.
    – Souvik Das
    Commented Jan 3, 2022 at 7:38

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.