2
2
3
3
namespace Drupal \os2forms_rest_api \Plugin \rest \resource ;
4
4
5
- use Drupal \Component \Plugin \Exception \InvalidPluginDefinitionException ;
6
- use Drupal \Component \Plugin \Exception \PluginNotFoundException ;
7
5
use Drupal \Core \Url ;
8
6
use Drupal \os2forms_rest_api \WebformHelper ;
9
7
use Drupal \rest \ModifiedResourceResponse ;
@@ -39,18 +37,18 @@ class WebformAllFormSubmissions extends ResourceBase {
39
37
private $ currentRequest ;
40
38
41
39
/**
42
- * The entity type manager object .
40
+ * The webform helper .
43
41
*
44
- * @var \Drupal\Core\Entity\EntityTypeManager
42
+ * @var \Drupal\os2forms_rest_api\WebformHelper
45
43
*/
46
- private $ entityTypeManager ;
44
+ private $ webformHelper ;
47
45
48
46
/**
49
- * The webform helper .
47
+ * The database connection .
50
48
*
51
- * @var \Drupal\os2forms_rest_api\WebformHelper
49
+ * @var \Drupal\Core\Database\Connection
52
50
*/
53
- private $ webformHelper ;
51
+ private $ database ;
54
52
55
53
/**
56
54
* {@inheritdoc}
@@ -60,9 +58,9 @@ class WebformAllFormSubmissions extends ResourceBase {
60
58
public static function create (ContainerInterface $ container , array $ configuration , $ plugin_id , $ plugin_definition ) {
61
59
$ instance = parent ::create ($ container , $ configuration , $ plugin_id , $ plugin_definition );
62
60
63
- $ instance ->entityTypeManager = $ container ->get ('entity_type.manager ' );
64
61
$ instance ->currentRequest = $ container ->get ('request_stack ' )->getCurrentRequest ();
65
62
$ instance ->webformHelper = $ container ->get (WebformHelper::class);
63
+ $ instance ->database = $ container ->get ('database ' );
66
64
67
65
return $ instance ;
68
66
}
@@ -112,32 +110,20 @@ public function get(string $webform_id): ModifiedResourceResponse {
112
110
113
111
$ result = ['webform_id ' => $ webform_id ];
114
112
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
-
132
113
$ requestQuery = $ this ->currentRequest ->query ;
133
114
115
+ // We use raw SQL to fetch submission sids and uuids.
116
+ // This is to avoid degraded performance by having to load every
117
+ // single submission through the webform submissions storage.
118
+ $ query = 'SELECT sid, uuid FROM webform_submission WHERE webform_id = :webform_id ' ;
119
+
134
120
foreach (self ::ALLOWED_DATETIME_QUERY_PARAMS as $ param => $ operator ) {
135
121
$ value = $ requestQuery ->get ($ param );
136
122
137
123
if (!empty ($ value )) {
138
124
try {
139
125
$ dateTime = new \DateTimeImmutable ($ value );
140
- $ submissionQuery -> condition ( ' created ' , $ dateTime ->getTimestamp (), $ operator );
126
+ $ query .= sprintf ( ' AND created %s %s ' , $ operator , $ dateTime ->getTimestamp ());
141
127
$ result [$ param ] = $ value ;
142
128
}
143
129
catch (\Exception $ e ) {
@@ -152,9 +138,10 @@ public function get(string $webform_id): ModifiedResourceResponse {
152
138
}
153
139
}
154
140
155
- // Complete query.
156
- $ submissionQuery ->accessCheck (FALSE );
157
- $ sids = $ submissionQuery ->execute ();
141
+ $ submissions = $ this ->database ->query (
142
+ $ query ,
143
+ [':webform_id ' => $ webform_id ]
144
+ )->fetchAllKeyed ();
158
145
159
146
// Generate submission URLs.
160
147
try {
@@ -163,12 +150,12 @@ public function get(string $webform_id): ModifiedResourceResponse {
163
150
'rest.webform_rest_submission.GET ' ,
164
151
[
165
152
'webform_id ' => $ webform_id ,
166
- 'uuid ' => $ submission-> uuid () ,
153
+ 'uuid ' => $ submission ,
167
154
]
168
155
)
169
156
->setAbsolute ()
170
157
->toString (TRUE )->getGeneratedUrl (),
171
- $ submissionEntityStorage -> loadMultiple ( $ sids ?: [])
158
+ $ submissions ?: []
172
159
);
173
160
}
174
161
catch (\Exception $ e ) {
0 commit comments