Skip to content

Commit 9651bfd

Browse files
committed
Avoided loading submissions when fetching all submissions
1 parent df8a972 commit 9651bfd

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ about writing changes to this log.
88

99
## [Unreleased]
1010

11+
## [2.2.0]
12+
13+
- Avoided loading submissions through `webform_submission` storage.
14+
1115
## [2.1.0]
1216

1317
- Drupal 10 compatibility.
@@ -40,7 +44,8 @@ about writing changes to this log.
4044

4145
- Release 1.0.0
4246

43-
[Unreleased]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.1.0...HEAD
47+
[Unreleased]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.2.0...HEAD
48+
[2.2.0]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.1.0...2.2.0
4449
[2.1.0]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.0.3...2.1.0
4550
[2.0.3]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.0.2...2.0.3
4651
[2.0.2]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.0.1...2.0.2

src/Plugin/rest/resource/WebformAllFormSubmissions.php

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ class WebformAllFormSubmissions extends ResourceBase {
5252
*/
5353
private $webformHelper;
5454

55+
/**
56+
* The database connection.
57+
*
58+
* @var \Drupal\Core\Database\Connection
59+
*/
60+
private $database;
61+
5562
/**
5663
* {@inheritdoc}
5764
*
@@ -63,6 +70,7 @@ public static function create(ContainerInterface $container, array $configuratio
6370
$instance->entityTypeManager = $container->get('entity_type.manager');
6471
$instance->currentRequest = $container->get('request_stack')->getCurrentRequest();
6572
$instance->webformHelper = $container->get(WebformHelper::class);
73+
$instance->database = $container->get('database');
6674

6775
return $instance;
6876
}
@@ -112,32 +120,17 @@ public function get(string $webform_id): ModifiedResourceResponse {
112120

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

115-
try {
116-
$submissionEntityStorage = $this->entityTypeManager->getStorage('webform_submission');
117-
}
118-
catch (InvalidPluginDefinitionException | PluginNotFoundException $e) {
119-
$errors = [
120-
'error' => [
121-
'message' => $this->t('Could not load webform submission storage'),
122-
],
123-
];
124-
125-
return new ModifiedResourceResponse($errors, Response::HTTP_INTERNAL_SERVER_ERROR);
126-
}
127-
128-
// Query for webform submissions with this webform_id.
129-
$submissionQuery = $submissionEntityStorage->getQuery()
130-
->condition('webform_id', $webform_id);
131-
132123
$requestQuery = $this->currentRequest->query;
133124

125+
$query = 'SELECT sid, uuid FROM webform_submission WHERE webform_id = :webform_id';
126+
134127
foreach (self::ALLOWED_DATETIME_QUERY_PARAMS as $param => $operator) {
135128
$value = $requestQuery->get($param);
136129

137130
if (!empty($value)) {
138131
try {
139132
$dateTime = new \DateTimeImmutable($value);
140-
$submissionQuery->condition('created', $dateTime->getTimestamp(), $operator);
133+
$query .= sprintf(' AND created %s %s', $operator, $dateTime->getTimestamp());
141134
$result[$param] = $value;
142135
}
143136
catch (\Exception $e) {
@@ -152,9 +145,10 @@ public function get(string $webform_id): ModifiedResourceResponse {
152145
}
153146
}
154147

155-
// Complete query.
156-
$submissionQuery->accessCheck(FALSE);
157-
$sids = $submissionQuery->execute();
148+
$submissions = $this->database->query(
149+
$query,
150+
[':webform_id' => $webform_id]
151+
)->fetchAllKeyed();
158152

159153
// Generate submission URLs.
160154
try {
@@ -163,12 +157,12 @@ public function get(string $webform_id): ModifiedResourceResponse {
163157
'rest.webform_rest_submission.GET',
164158
[
165159
'webform_id' => $webform_id,
166-
'uuid' => $submission->uuid(),
160+
'uuid' => $submission,
167161
]
168162
)
169163
->setAbsolute()
170164
->toString(TRUE)->getGeneratedUrl(),
171-
$submissionEntityStorage->loadMultiple($sids ?: [])
165+
$submissions ?: []
172166
);
173167
}
174168
catch (\Exception $e) {

0 commit comments

Comments
 (0)