-1

I created a custom webform handler that on postSave adds webform id to the queue:

public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {

$item = array(
  'submission' => $webform_submission,
);

// Add the item to the queue.
$queue_factory = \Drupal::service('queue');
$queue = $queue_factory->get('custom_send_emails');
$queue->createItem($item);

} } This is the QueueWorker:

/**
 * Send Emails
 *
 * @QueueWorker(
 *   id = "custom_send_emails",
 *   title = @Translation("send emails"),
 *   cron = {"time" = 120}
 * )
 */
class CustomEmailQueue extends CustomEmailQueueBase {}

and this is the QueueWorkerBase that on processItem send the email that is build by a custom mail handler:

public function processItem($item) {
    $this->custom_send_emails($item['submission']);
  }

  function custom_form_send_emails(WebformSubmissionInterface $submission) {
    $webform = $submission->getWebform();
    $handler = $webform->getHandler('local_email');
    $handler->sendMessage($submission);
  }

I have tried everything! When Cron runs, it takes the first item of the queue create the message correctly and send the email with the attachments. Then it continues to the rest of the items and for all of them it doesn't build correct the message. The to_email is empty. The $attachments array of the $message is empty.

Why does it do that? Why the first item of the queue is send correctly and the rest fail?

With Debug I found that in the first queue item process, it calls from EmailWebformHandler.php the getMessageAttachments() and the getElementsInitializedAndFlattened fetches more elements(the attachments and everything) than in next calls? WHY?

Eventually I found that this segment of code in my local_email handler causes the problem and I don't understand why. In this handler I want to send the email in the language selected in one specific field. This is why I get the language from the field and then I do this

$this->languageManager->setConfigOverrideLanguage($language);

that causes the issue!!!

 //get communication language
    $values = $webform_submission->getData();
    $language = $this->languageManager->getLanguage($values['communication_language']);
    $this->languageManager->setConfigOverrideLanguage($language);
    $mail_config = \Drupal::config('webform.webform.sme_fund');
    $subject = $mail_config->get('handlers.local_email.settings.subject');
    $subject = $this->replaceTokens($subject, $webform_submission);
    $body = $mail_config->get('handlers.local_email.settings.body');
    $current_langcode = $values['communication_language'];
    $message['subject'] = $subject;
    $message['body'] = $body;
0
0

If your code works when you don't change langage, that could be that you just forgot to restore the original langage after processing - as it is the same process running, it means that the next item in your queue will be processed with a langage manager set from the previous item which could be different.

Try to do something like :

$original_language = $this->languageManager->getConfigOverrideLanguage();
// your processing code
$this->languageManager->setConfigOverrideLanguage($original_language);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.