Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOJ-120: Queue webform submissions #79

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions docroot/modules/custom/foia_webform/foia_webform.module
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@
use Drupal\webform\WebformInterface;

/**
* Programmatically add the foia_email handler to new webforms.
* Programmatically add the foia_submission_queue handler to new webforms.
*/
function foia_webform_webform_insert(WebformInterface $webform) {
$webform_handler_manager = \Drupal::service('plugin.manager.webform.handler');
$webform_handler = $webform_handler_manager->createInstance('foia_email');
$webform_handler = $webform_handler_manager->createInstance('foia_submission_queue');

// @todo: Add Submission Email if we add Agency Component entity reference.
// Set default configuration.
$webform_handler->setConfiguration([
'id' => 'foia_email',
'label' => 'FOIA Email',
'handler_id' => 'foia_email',
'id' => 'foia_submission_queue',
'label' => 'FOIA Submission Queue',
'handler_id' => 'foia_submission_queue',
'status' => TRUE,
'weight' => 0,
'settings' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Drupal\foia_webform\Plugin\QueueWorker;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides functionality for working with the queued FOIA form submissions.
*
* @QueueWorker (
* id = "foia_submissions",
* title = @Translation("FOIA Submission Queue Worker"),
* )
*/
class FoiaSubmissionQueueWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {

/**
* The webform storage.
*
* @var \Drupal\webform\WebformStorageInterface
*/
protected $webformStorage;

/**
* {@inheritdoc}
*/
public function __construct(WebformStorageInterface $webformStorage) {
$this->webformStorage = $webformStorage;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$container->get('entity.manager')->getStorage('webform_submission')
);
}

/**
* {@inheritdoc}
*/
public function processItem($data) {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Drupal\foia_webform\Plugin\WebformHandler;

use Drupal\webform\Plugin\WebformHandler\EmailWebformHandler;
use Drupal\webform\WebformSubmissionInterface;

/**
* Emails a webform submission.
*
* @WebformHandler(
* id = "foia_submission_queue",
* label = @Translation("FOIA Submission Queue"),
* category = @Translation("Queueing"),
* description = @Translation("Queues a webform submission to be sent later."),
* cardinality = \Drupal\webform\WebformHandlerInterface::CARDINALITY_UNLIMITED,
* results = \Drupal\webform\WebformHandlerInterface::RESULTS_PROCESSED,
* )
*/
class FoiaSubmissionQueueHandler extends EmailWebformHandler {

/**
* {@inheritdoc}
*/
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {
$state = $webform_submission->getWebform()->getSetting('results_disabled') ? WebformSubmissionInterface::STATE_COMPLETED : $webform_submission->getState();
if (in_array($state, $this->configuration['states'])) {
// Get the Webform generated email message.
$message = $this->getMessage($webform_submission);
// Add the submission to the queue.
$this->queueSubmission($webform_submission, $message);
}
}

/**
* Adds the submission to the foia_submissions queue.
*
* @param \Drupal\webform\WebformSubmissionInterface $webform_submission
* The webform submission object.
* @param array $message
* The email message array.
*/
private function queueSubmission(WebformSubmissionInterface $webform_submission, array $message) {
// @var QueueFactory $queue_factory
$queue_factory = \Drupal::service('queue');

// @var QueueInterface $queue
$foia_submission_queue = $queue_factory->get('foia_submissions');
$submission = new \stdClass();
$submission->sid = $webform_submission->id();
$submission->message = $message;

// Log the form submission.
\Drupal::logger('foia_webform')
->info('FOIA form submission #%sid added to queue.',
[
'%sid' => $webform_submission->id(),
'link' => $webform_submission->toLink($this->t('View'))->toString(),
]
);

$foia_submission_queue->createItem($submission);
}

}