3 of 3 edited tags

Programmatically delete a webform submission by sid without first loading it

Drupal 8 project has a 2 webforms:

  • Webform "ABC"
  • Webform "123"

Explanation of the process:

When Webform "ABC" is submitted, a new "node" is created and the "Sid" used to create this node is recorded inside a "text" field (field_submission_sid) on this created node.

Now, when Webform "123" is loaded, the value of (field_submission_sid) is populated inside a 'text' element (submission_to_delete)... and when the Webform "123" is submitted, a custom handler will delete the submission of sid value = (submission_to_delete)

The custom handler is working but I believe there is a better solution... so please I need your help ?

Custom handler:

<?php

namespace Drupal\custom_module\Plugin\WebformHandler;

use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Form\FormStateInterface;

use Drupal\node\Entity\Node;

use Drupal\webform\WebformInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\webformSubmissionInterface;
use Drupal\webform\Entity\WebformSubmission;


/**
 * Delete a 'webform Submission' using its "SID" retrieved from a populated webform submission value.
 *
 * @WebformHandler(
 *   id = "Delete a webform Submission using its sid",
 *   label = @Translation("Delete a webform Submission using its sid"),
 *   category = @Translation("Entity Delete"),
 *   description = @Translation("Delete a webform Submission using its sid retrieved from a populated webform submission valu"),
 *   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
 *   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 *   submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
 * )
 */

class DeleteSidWebformHandler extends WebformHandlerBase {

  /**
   * {@inheritdoc}
   */

  // Function to be fired while submitting the Webform.
  public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
    // Get an array of the values from the submission.
    $values = $webform_submission->getData();

    // Load 'submission' object from webform-submission populated data. 
    //get (SID) from (submission_to_delete) text element available on the webform
    $sid = $values["submission_to_delete"];
    $webform_submission = \Drupal\webform\Entity\WebformSubmission::load($sid);

    //Delete the Submission
    $webform_submission->delete();
 }
}

If you notice, I am loading the submission first then i am deleting it...

is there a better solution where I can delete the submission directly without first have to load it ? maybe something like $webform_submission = \Drupal\webform\Entity\WebformSubmission::delete(submission_to_delete);

UPDATE:

I noticed if the 'webform submission' is not available (Ex: An admin deleted by mistake) so the value of $sid will be NULL and the below error is generated:

Error: Call to a member function delete() on null in Drupal\custom_module\Plugin\WebformHandler\DeleteSidWebformHandler->submitForm()

How to add an if statement to check if $sid value is empty then do nothing otherwise execute code.

Thank you,