Score:0

Webform Create node, send email when webform is submitted

us flag

I'm creating a node when a webform is submitted and adding the webform submission id to an int field on the node so the node is linked to the webform submission. I'm also sending an email when the webform is submitted.

Here's the node creation handler code.

<?php

namespace Drupal\my_module\Plugin\WebformHandler;

use Drupal;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\Node;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\webformSubmissionInterface;

/**
 * Create Award Nominee nodes from a webform submission.
 *
 * @WebformHandler(
 *   id = "award_nominee_from_webform",
 *   label = @Translation("Award Nominee node"),
 *   category = @Translation("Webform"),
 *   description = @Translation("Creates an Award Nominee node."),
 *   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
 *   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 *   submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
 * )
 */

class AwardNomineeWebformHandler extends WebformHandlerBase {

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
    $webform = $webform_submission->getWebform();
    // Get an array of form field values.
    $submission_array = $webform_submission->getData();
    // Todo: Prevent this save from sending emails.
    $webform_submission->save();

    $sid = $webform_submission->id();

    $term = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->load($submission_array['nomination_award']);
    $award_name = $term->getName();

    if (empty($submission_array['member_information'])) {
      $nominee_middle_name = !empty($submission_array['nominee_middle_name']) ? ' ' . $submission_array['nominee_middle_name'] . ' ' : ' ';
      $title = $submission_array['nominee_first_name'] . $nominee_middle_name . $submission_array['nominee_last_name'];
      $nominee_accomplishments = $submission_array['nominee_accomplishments'];

      $this->getNode($title, $award_name, $nominee_accomplishments, $sid);
    }
  }

  /**
   * Creates the node when it's a new webform submission.
   *
   * @param string $title
   * @param string $award_name
   * @param mixed $nominee_accomplishments
   * @param int $sid
   *
   * @return void
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  protected function getNode(string $title, string $award_name, mixed $nominee_accomplishments, int $sid): void {
    $node = Node::create([
      'type' => 'award_nominee',
      'status' => FALSE,
      'title' => $title,
      'field_award_nominee_plaque_name' => '',
      'field_award_nominee_award_name' => $award_name,
      'field_award_nominee_organization' => '',
      'field_award_nominee_members' => '',
      'body' => [
        'value' => $nominee_accomplishments,
        'format' => 'bold_italic',
      ],
      'field_webform_submission_id' => $sid,
      'field_award_nominee_city' => '',
      'field_award_nominee_state' => '',
    ]);
    $node->save();
  }

}

There's even a hook to delete the nodes when the submission is deleted. Here's the hook:

/**
 * Implements hook_webform_submission_delete().
 */
function my_module_webform_submission_delete(WebformSubmissionInterface $webform_submission) {
  $entity_ids = Drupal::entityQuery('node')
    ->condition('type', 'award_nominee')
    ->condition('field_webform_submission_id.value', $webform_submission->id())
    ->execute();
  $nodes = Node::loadMultiple($entity_ids);
  foreach ($nodes as $node) {
    $node->delete();
  }
}

Although I think another webform handler could "handle" the deletions too.

The problem is the emails are being sent: one from the configured email handler, and one from the webform_submission->save() in the node creation handler. How can I prevent an email being sent from the node creation handler? $webform_submission->resave() seems to solve the problem like this.

$sid = '';
if ($webform_submission->id()) {
  $webform_submission->resave();
  $sid = $webform_submission->id();
}
Score:1
hm flag

Use \Drupal\webform\Entity\WebformSubmission::resave instead of \Drupal\webform\Entity\WebformSubmission::save

us flag
Thanks @Vanish. That helps a lot. I'll update the code with your suggestion and approve your comment.
I sit in a Tesla and translated this thread with Ai:

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.