22
33namespace Drupal \os2forms_rest_api \Plugin \rest \resource ;
44
5- use Drupal \Component \Plugin \Exception \InvalidPluginDefinitionException ;
6- use Drupal \Component \Plugin \Exception \PluginNotFoundException ;
75use Drupal \Core \Url ;
86use Drupal \os2forms_rest_api \WebformHelper ;
97use Drupal \rest \ModifiedResourceResponse ;
@@ -39,18 +37,18 @@ class WebformAllFormSubmissions extends ResourceBase {
3937 private $ currentRequest ;
4038
4139 /**
42- * The entity type manager object .
40+ * The webform helper .
4341 *
44- * @var \Drupal\Core\Entity\EntityTypeManager
42+ * @var \Drupal\os2forms_rest_api\WebformHelper
4543 */
46- private $ entityTypeManager ;
44+ private $ webformHelper ;
4745
4846 /**
49- * The webform helper .
47+ * The database connection .
5048 *
51- * @var \Drupal\os2forms_rest_api\WebformHelper
49+ * @var \Drupal\Core\Database\Connection
5250 */
53- private $ webformHelper ;
51+ private $ database ;
5452
5553 /**
5654 * {@inheritdoc}
@@ -60,9 +58,9 @@ class WebformAllFormSubmissions extends ResourceBase {
6058 public static function create (ContainerInterface $ container , array $ configuration , $ plugin_id , $ plugin_definition ) {
6159 $ instance = parent ::create ($ container , $ configuration , $ plugin_id , $ plugin_definition );
6260
63- $ instance ->entityTypeManager = $ container ->get ('entity_type.manager ' );
6461 $ instance ->currentRequest = $ container ->get ('request_stack ' )->getCurrentRequest ();
6562 $ instance ->webformHelper = $ container ->get (WebformHelper::class);
63+ $ instance ->database = $ container ->get ('database ' );
6664
6765 return $ instance ;
6866 }
@@ -112,32 +110,20 @@ public function get(string $webform_id): ModifiedResourceResponse {
112110
113111 $ result = ['webform_id ' => $ webform_id ];
114112
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-
132113 $ requestQuery = $ this ->currentRequest ->query ;
133114
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+
134120 foreach (self ::ALLOWED_DATETIME_QUERY_PARAMS as $ param => $ operator ) {
135121 $ value = $ requestQuery ->get ($ param );
136122
137123 if (!empty ($ value )) {
138124 try {
139125 $ dateTime = new \DateTimeImmutable ($ value );
140- $ submissionQuery -> condition ( ' created ' , $ dateTime ->getTimestamp (), $ operator );
126+ $ query .= sprintf ( ' AND created %s %s ' , $ operator , $ dateTime ->getTimestamp ());
141127 $ result [$ param ] = $ value ;
142128 }
143129 catch (\Exception $ e ) {
@@ -152,9 +138,10 @@ public function get(string $webform_id): ModifiedResourceResponse {
152138 }
153139 }
154140
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 ();
158145
159146 // Generate submission URLs.
160147 try {
@@ -163,12 +150,12 @@ public function get(string $webform_id): ModifiedResourceResponse {
163150 'rest.webform_rest_submission.GET ' ,
164151 [
165152 'webform_id ' => $ webform_id ,
166- 'uuid ' => $ submission-> uuid () ,
153+ 'uuid ' => $ submission ,
167154 ]
168155 )
169156 ->setAbsolute ()
170157 ->toString (TRUE )->getGeneratedUrl (),
171- $ submissionEntityStorage -> loadMultiple ( $ sids ?: [])
158+ $ submissions ?: []
172159 );
173160 }
174161 catch (\Exception $ e ) {
0 commit comments