diff --git a/CHANGELOG.md b/CHANGELOG.md index 69dab2e..f3b5636 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. @@ -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 diff --git a/src/Plugin/rest/resource/WebformAllFormSubmissions.php b/src/Plugin/rest/resource/WebformAllFormSubmissions.php index e1c8d39..9dd41b6 100644 --- a/src/Plugin/rest/resource/WebformAllFormSubmissions.php +++ b/src/Plugin/rest/resource/WebformAllFormSubmissions.php @@ -52,6 +52,13 @@ class WebformAllFormSubmissions extends ResourceBase { */ private $webformHelper; + /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + private $database; + /** * {@inheritdoc} * @@ -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; } @@ -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) { @@ -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 { @@ -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) {