Skip to content

Commit

Permalink
SUPP0RT-1235: Added submission helper command
Browse files Browse the repository at this point in the history
  • Loading branch information
rimi-itk committed Sep 21, 2023
1 parent 56f4056 commit 84a904a
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

Nedenfor ses dato for release og beskrivelse af opgaver som er implementeret.

## [Unreleaed]

* [#218](https://github.com/itk-dev/os2forms_selvbetjening/pull/218)
Added submission helper command

## [2.4.9] 2023-09-06

* Tilføjede webform options config ignore.
Expand Down
7 changes: 7 additions & 0 deletions web/modules/custom/os2forms_selvbetjening/drush.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
Drupal\os2forms_selvbetjening\Command\WebformSubmissionCommands:
arguments:
- '@entity_type.manager'
- '@database'
tags:
- { name: drush.command }
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Drupal\os2forms_selvbetjening\Command;

use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drush\Commands\DrushCommands;
use Symfony\Component\Yaml\Yaml;

/**
* Webform submission commands.
*/
class WebformSubmissionCommands extends DrushCommands {

/**
* Constructor.
*/
public function __construct(
private readonly EntityTypeManagerInterface $entityTypeManager,
private readonly Connection $database
) {
}

/**
* List stray submissions.
*
* @param string $webformId
* The webform id.
* @param array $options
* The command options.
*
* @option bool fix
* Fix webform revision, i.e. set it to null, on stray submissions.
*
* @command os2forms_selvbetjening:webform-submission:list-stray-submissions
*
* @phpstan-param array<string, mixed> $options
*/
public function listStraySubmissions(string $webformId, array $options = [
'fix' => FALSE,
]) {
/** @var \Drupal\webform\WebformSubmissionStorageInterface $submissionStorage */
$submissionStorage = $this->entityTypeManager->getStorage('webform_submission');
$webformStorage = $this->entityTypeManager->getStorage('webform');

$webform = $webformStorage->load($webformId);

/** @var \Drupal\webform\WebformSubmissionInterface[] $submissions */
$submissions = $submissionStorage->loadByProperties([
'webform_id' => $webformId,
]);

foreach ($submissions as $submission) {
if ($webform->id() !== $submission->getWebform()->id()) {
$this->writeln(
Yaml::dump([
'id' => $submission->id(),
'webform_id' => $submission->getWebform()->id(),
'data' => $submission->getData(),
], PHP_INT_MAX)
);

if ($options['fix']) {
$this->database->update('webform_submission')
->fields([
'webform_revision' => NULL,
])
->condition('sid', $submission->id())
->execute();

$this->writeln(sprintf('Fixed webform revision on submission %s', $submission->id()));
}
}
}
}

}

0 comments on commit 84a904a

Please sign in to comment.