Created
July 20, 2019 21:18
-
-
Save DanielVeza/f2b23525939428ebe8f48450abdda427 to your computer and use it in GitHub Desktop.
Custom webform action - Download submissions from multiple webforms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Made on request from a user on drupal slack. | |
// Not fully tested - I've stripped this from a module I made from a client and not tested it still works after | |
// removing some functionality specific to that site. | |
// *Should work* and be a good starting point at least. | |
// Put this in a custom module - The example I took this from was called webfrom_extras. | |
// This may need minor modification - I've scrubbed data from it. | |
// Let me know if you have any issues. | |
namespace Drupal\webform_extras\Plugin\Action; | |
use Drupal\Core\Action\ActionBase; | |
use Drupal\Core\Session\AccountInterface; | |
use Drupal\Core\Url; | |
use Drupal\webform\Controller\WebformResultsExportController; | |
use Drupal\webform\Entity\Webform; | |
use Drupal\webform\Entity\WebformSubmission; | |
use Symfony\Component\HttpFoundation\BinaryFileResponse; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
/** | |
* Locks a webform submission. | |
* | |
* @Action( | |
* id = "webform_submission_download_action", | |
* label = @Translation("Download submission"), | |
* type = "webform_submission" | |
* ) | |
*/ | |
class DownloadSubmission extends ActionBase { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function execute($entity = NULL) { | |
$webformId = $entity->getWebform()->id(); | |
$sub = WebformSubmission::loadMultiple([$entity->id()]); | |
/** @var \Drupal\webform\WebformInterface $webform */ | |
$webform = Webform::load($webformId); | |
/** @var \Drupal\Core\Entity\EntityInterface|null $source_entity */ | |
$source_entity = \Drupal::entityTypeManager()->getStorage('webform_submission')->load($entity->id()); | |
/** @var \Drupal\webform\WebformSubmissionExporterInterface $submission_exporter */ | |
$submission_exporter = \Drupal::service('webform_submission.exporter'); | |
$submission_exporter->setWebform($webform); | |
$default_options = $submission_exporter->getWebformOptions(); | |
/** @var array $export_options */ | |
$export_options = $default_options; | |
// Always use Excel for this. | |
$export_options['excel'] = 1; | |
// Write the file. | |
$submission_exporter->setSourceEntity($source_entity); | |
$submission_exporter->setExporter($export_options); | |
$submission_exporter->generate(); | |
$submission_exporter->writeHeader(); | |
$submission_exporter->writeRecords($sub); | |
$submission_exporter->writeFooter(); | |
$submission_exporter->writeExportToArchive(); | |
// Download the file. | |
$file_path = $submission_exporter->getArchiveFileName(); | |
$params = [ | |
'webform' => $webformId, | |
'filename' => $file_path, | |
]; | |
$route_name = 'entity.webform.results_export_file'; | |
$redirect_url = Url::fromRoute($route_name, $params, ['absolute' => TRUE])->toString(); | |
$resp = new RedirectResponse($redirect_url); | |
$resp->send(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function executeMultiple(array $entities) { | |
// There is a bit of code duplication here between execute and | |
// executemultiple. Lets rethink that. | |
$existingWebformID = ''; | |
// Scrubbed this. | |
$processingShouldContinue = true; | |
if ($processingShouldContinue) { | |
// Can we DI this? | |
$exporter = \Drupal::service('webform_submission.exporter'); | |
// The important settings are all the same for all webforms at the moment | |
// so we can just load the first. | |
$webformId = current($entities)->getWebform()->id(); | |
/** @var \Drupal\webform\WebformInterface $webform */ | |
$webform = \Drupal::entityTypeManager()->getStorage('webform')->load($webformId); | |
$exporter->setWebform($webform); | |
/** @var \Drupal\Core\Entity\EntityInterface|null $source_entity */ | |
$source_entity = \Drupal::entityTypeManager()->getStorage('webform_submission')->load(current($entities)->id()); | |
// Can we DI this? | |
/** @var \Drupal\webform\WebformSubmissionExporterInterface $submission_exporter */ | |
$submission_exporter = \Drupal::service('webform_submission.exporter'); | |
$submission_exporter->setWebform($webform); | |
// Get the default option for the webform. | |
$default_options = $submission_exporter->getWebformOptions(); | |
$export_options = $default_options; | |
// Always use Excel for this. | |
$export_options['excel'] = 1; | |
// Create the export. | |
$submission_exporter->setSourceEntity($source_entity); | |
$submission_exporter->setExporter($export_options); | |
$submission_exporter->generate(); | |
$submission_exporter->writeHeader(); | |
$submission_exporter->writeRecords($entities); | |
$submission_exporter->writeFooter(); | |
$submission_exporter->writeExportToArchive(); | |
// Download the file. | |
$file_path = $submission_exporter->getArchiveFileName(); | |
$params = [ | |
'webform' => $webformId, | |
'filename' => $file_path, | |
]; | |
$route_name = 'entity.webform.results_export_file'; | |
$redirect_url = Url::fromRoute($route_name, $params, ['absolute' => TRUE])->toString(); | |
$resp = new RedirectResponse($redirect_url); | |
$resp->send(); | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { | |
/** @var \Drupal\webform\WebformSubmissionInterface $object */ | |
$result = $object->locked->access('edit', $account, TRUE) | |
->andIf($object->access('update', $account, TRUE)); | |
return $return_as_object ? $result : $result->isAllowed(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment