Score:0

Send email to author when content is published through scheduler

rs flag

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.

Score:0
cn flag

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;
  }
}
Souvik Das avatar
rs flag
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 avatar
rs flag
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, https://drupal.stackexchange.com/questions/237353/how-do-i-pass-parameters-to-a-service services are not suitable in cases where I need to pass parameters from the service call.
Alireza Tabatabaeian avatar
cn flag
to answer your first question you can go to scheduler module in src/SchedulerEvents.php you can find the list of events
Alireza Tabatabaeian avatar
cn flag
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()
Souvik Das avatar
rs flag
Getting the same issue with your solution. Email sent but node not published. Once I remove the mail sending code, node is published.
Christophe CARON avatar
in flag
To answer to Souvik Das, I found this in the module: * The six possible Scheduler events are: * SchedulerEvents::PRE_PUBLISH * SchedulerEvents::PUBLISH * SchedulerEvents::PRE_UNPUBLISH * SchedulerEvents::UNPUBLISH * SchedulerEvents::PRE_PUBLISH_IMMEDIATELY * SchedulerEvents::PUBLISH_IMMEDIATELY.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.