Skip to content

Commit

Permalink
Avoided loading submissions when fetching all submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
jekuaitk committed Dec 17, 2024
1 parent df8a972 commit 9651bfd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ about writing changes to this log.

## [Unreleased]

## [2.2.0]

- Avoided loading submissions through `webform_submission` storage.

## [2.1.0]

- Drupal 10 compatibility.
Expand Down Expand Up @@ -40,7 +44,8 @@ about writing changes to this log.

- Release 1.0.0

[Unreleased]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.1.0...HEAD
[Unreleased]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.2.0...HEAD
[2.2.0]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.1.0...2.2.0
[2.1.0]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.0.3...2.1.0
[2.0.3]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.0.2...2.0.3
[2.0.2]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.0.1...2.0.2
Expand Down
40 changes: 17 additions & 23 deletions src/Plugin/rest/resource/WebformAllFormSubmissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class WebformAllFormSubmissions extends ResourceBase {
*/
private $webformHelper;

/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
private $database;

/**
* {@inheritdoc}
*
Expand All @@ -63,6 +70,7 @@ public static function create(ContainerInterface $container, array $configuratio
$instance->entityTypeManager = $container->get('entity_type.manager');
$instance->currentRequest = $container->get('request_stack')->getCurrentRequest();
$instance->webformHelper = $container->get(WebformHelper::class);
$instance->database = $container->get('database');

return $instance;
}
Expand Down Expand Up @@ -112,32 +120,17 @@ public function get(string $webform_id): ModifiedResourceResponse {

$result = ['webform_id' => $webform_id];

try {
$submissionEntityStorage = $this->entityTypeManager->getStorage('webform_submission');
}
catch (InvalidPluginDefinitionException | PluginNotFoundException $e) {
$errors = [
'error' => [
'message' => $this->t('Could not load webform submission storage'),
],
];

return new ModifiedResourceResponse($errors, Response::HTTP_INTERNAL_SERVER_ERROR);
}

// Query for webform submissions with this webform_id.
$submissionQuery = $submissionEntityStorage->getQuery()
->condition('webform_id', $webform_id);

$requestQuery = $this->currentRequest->query;

$query = 'SELECT sid, uuid FROM webform_submission WHERE webform_id = :webform_id';

foreach (self::ALLOWED_DATETIME_QUERY_PARAMS as $param => $operator) {
$value = $requestQuery->get($param);

if (!empty($value)) {
try {
$dateTime = new \DateTimeImmutable($value);
$submissionQuery->condition('created', $dateTime->getTimestamp(), $operator);
$query .= sprintf(' AND created %s %s', $operator, $dateTime->getTimestamp());
$result[$param] = $value;
}
catch (\Exception $e) {
Expand All @@ -152,9 +145,10 @@ public function get(string $webform_id): ModifiedResourceResponse {
}
}

// Complete query.
$submissionQuery->accessCheck(FALSE);
$sids = $submissionQuery->execute();
$submissions = $this->database->query(
$query,
[':webform_id' => $webform_id]
)->fetchAllKeyed();

// Generate submission URLs.
try {
Expand All @@ -163,12 +157,12 @@ public function get(string $webform_id): ModifiedResourceResponse {
'rest.webform_rest_submission.GET',
[
'webform_id' => $webform_id,
'uuid' => $submission->uuid(),
'uuid' => $submission,
]
)
->setAbsolute()
->toString(TRUE)->getGeneratedUrl(),
$submissionEntityStorage->loadMultiple($sids ?: [])
$submissions ?: []
);
}
catch (\Exception $e) {
Expand Down

0 comments on commit 9651bfd

Please sign in to comment.