0

I have been digging around for about a week trying to figure this out and still have not come across anything that works or is even close.

The idea is that I have a custom webform. This webform has an Remote POST submit handler. Part of that submission and submit handler puts a value from the response into the webform via TOKENS. If an Remote POST submission fails, this value will be empty. EDIT FOR CLARITY: The default value for this "api_response" field (which is hidden on the form) is the token

[webform:handler:remote_post:completed:0:inserted:[webform_submission:values:external_id]][webform:handler:remote_post_1:completed:0:inserted:[webform_submission:values:external_id]]

In a CRON job, a query is run to see which webform submissions do not have this value and then it adds them to a Queue.

The queue worker then loops through and simply attempts to resubmit the Webform Submission if it failed earlier for whatever reason. By my initial understanding, when the submission update is submited, it should trigger the handlers. However, in the programmatic Cron, submission update does not appear to trigger any handlers. I have found some stuff for simple email handlers, but they do not have remotely the same setup or methods for \Drupal\webform\Plugin\WebformHandler\RemotePostWebformHandler. No idea how to ensure this gets re-ran with a webform submission update.

This is my queue worker file.

<?php
namespace Drupal\my_module\Plugin\QueueWorker;

use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\webform\Plugin\WebformHandler\RemotePostWebformHandler;
use Drupal\webform\WebformSubmissionForm;

/**
 * Product Offer Scheduler Queue Worker.
 *
 * @QueueWorker(
 *   id = "webform_submission_replay_queue_worker",
 *   title = @Translation("Webform Submission Replay Queue Worker: WebformSubmission"),
 *   cron = {"time" = 180}
 * )
 */
class WebformSubmissionReplayQueueWorker extends QueueWorkerBase {

    /**
     * A function to run the appropriate handlers on each webform.
     *
     * @param WebformSubmission $webformSubmission
     * @param string $webformId
     *
     * @throws EntityStorageException
     */
    private function processWebformSubmission($webformSubmission, $webformId) {
        switch ($webformId) {
            case 'form_id':
                $accountId = $webformSubmission->getElementData('account_id');
                $accountType = $webformSubmission->getElementData('account_type');
                $webform = $webformSubmission->getWebform();
//                $webform->getState();
                if ($accountId == "" || $accountType != "customer") {
//                    $config = \Drupal::configFactory()->getEditable('webform.webform.' . $webformId);
//                    $config_key = 'handlers.remote_post';
//                    $config->
//                    $webform->addWebformHandler($webformSubmission->getWebform()->getHandler('remote_post'));
//                    $webform->save();
//                    /** @var RemotePostWebformHandler $handler */
//                    $handler = $webformSubmission->getWebform()->getHandler('remote_post');
//                    $handler->setWebformSubmission($webformSubmission);
//                    $handler->submitForm();
//                    $message = $handler->getMessage($webformSubmission);
//                    $handler->sendMessage($webformSubmission, $message);
                } elseif ($accountId != "" && $accountType == "customer") {
//                    $webform->addWebformHandler($webformSubmission->getWebform()->getHandler('remote_post_1'));
//                    $webform->save();
//                    $handler = $webformSubmission->getWebform()->getHandler('remote_post_1');
//                    $handler->setWebformSubmission($webformSubmission);
//                    $handler->submitForm();
//                    $message = $handler->getMessage($webformSubmission);
//                    $handler->sendMessage($webformSubmission, $message);
                }
                break;
            default:
                // do nothing
        }
    }

    public function processItem($data) {
        $webformId = $data->webform_id;
        $submissionSerial = $data->serial;

        $webform = Webform::load($webformId);
        $isOpen = WebformSubmissionForm::isOpen($webform);

        if($isOpen === TRUE){
            $webformSubmission = WebformSubmission::load($submissionSerial);

//            $token = \Drupal::token();
//            $elem = $webform->getElement('api_response');
//            $default = $token->replace($elem['#value']);
//            \Drupal::logger('my_module')->debug('tokens: <pre>@res</pre>', [
//                '@res' => $token->replace($elem['#value'])
//            ]);
//            $webformSubmission->setElementData('api_response', $token->replace($elem['#value']));
//            $fields = $webformSubmission->getFields();

//            \Drupal::logger('my_module_'.$webformId)->debug('Fields: <pre>@data</pre>', [
//                '@data' => print_r(array_keys($fields), true)
//            ]);
            $errors = WebformSubmissionForm::validateWebformSubmission($webformSubmission);
            if(!empty($errors)) {
                \Drupal::logger('my_module_reroll_errors_'.$webformId)
                    ->debug('Submission ID: @id - Errors: <pre>@err</pre>', [
                        '@id' => $submissionSerial,
                        '@err' => print_r($errors, true)
                    ]);
            } else {
//                $ws = WebformSubmission::;
//                $webformSubmission = WebformSubmissionForm::submitWebformSubmission($webformSubmission);
//                $webformSubmission->save();
//                $handlers = $webform->getHandlers();
//                \Drupal::logger('my_module')->debug('tokens: <pre>@res</pre>', [
//                    '@res' => $token->replace($elem['#value'])
//                ]);
                $this->processWebformSubmission($webformSubmission, $webformId);
                \Drupal::logger('my_module_reroll_'.$webformId)->debug('sid: @sid --- serial: @serial', [
                    '@sid' => $webformSubmission->id(),
                    '@serial' => $submissionSerial
                ]);
            }
        }

    }
}
2
  • is this if clause ok? if(!empty($errors)), this check will pass and just log the message if there's no errors. Commented Oct 19, 2022 at 16:02
  • This clause is intended to capture webform submissions that for whatever reason are saved but do not meet the requirements to be submitted. This will be flushed out later to provide more information. Commented Oct 19, 2022 at 17:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.