99use Drupal \maestro \Form \MaestroExecuteInteractive ;
1010use Drupal \maestro \Engine \MaestroEngine ;
1111use Drupal \Core \Form \FormStateInterface ;
12+ use Drupal \webform \WebformSubmissionInterface ;
1213use Symfony \Component \HttpFoundation \RedirectResponse ;
1314
1415/**
@@ -74,6 +75,12 @@ public function getTaskEditForm(array $task, $templateMachineName) {
7475 '#default_value ' => $ task ['data ' ]['inherit_webform_unique_id ' ] ?? '' ,
7576 '#required ' => TRUE ,
7677 ];
78+ $ form ['inherit_webform_create_submission ' ] = [
79+ '#type ' => 'checkbox ' ,
80+ '#title ' => $ this ->t ('Create submission ' ),
81+ '#description ' => $ this ->t ('Create submission ' ),
82+ '#default_value ' => $ task ['data ' ]['inherit_webform_create_submission ' ] ?? FALSE ,
83+ ];
7784 return $ form ;
7885 }
7986
@@ -86,6 +93,7 @@ public function prepareTaskForSave(array &$form, FormStateInterface $form_state,
8693 parent ::prepareTaskForSave ($ form , $ form_state , $ task );
8794 // Add custom field(s) to the inherited prepareTaskForSave method.
8895 $ task ['data ' ]['inherit_webform_unique_id ' ] = $ form_state ->getValue ('inherit_webform_unique_id ' );
96+ $ task ['data ' ]['inherit_webform_create_submission ' ] = $ form_state ->getValue ('inherit_webform_create_submission ' );
8997 }
9098
9199 /**
@@ -120,48 +128,121 @@ public function getExecutableForm($modal, MaestroExecuteInteractive $parent) {
120128 }
121129 // Now create webform submission, submit and attach to current process.
122130 $ templateTask = MaestroEngine::getTemplateTaskByQueueID ($ this ->queueID );
123- $ taskUniqueSubmissionId = $ templateTask ['data ' ]['unique_id ' ];
124131 $ webformMachineName = $ templateTask ['data ' ]['webform_machine_name ' ];
125132
126133 $ values = [];
127134 $ values ['webform_id ' ] = $ webformMachineName ;
128135 $ values ['data ' ] = $ field_values ;
129136
130- // Create submission.
131- $ new_submission = WebformSubmission::create ($ values );
137+ $ createSubmission = (bool ) ($ task ['data ' ]['inherit_webform_create_submission ' ] ?? FALSE );
138+ if ($ createSubmission ) {
139+ // Create submission.
140+ $ new_submission = WebformSubmission::create ($ values );
141+
142+ // Submit the webform submission.
143+ $ submission = WebformSubmissionForm::submitWebformSubmission ($ new_submission );
144+
145+ // WebformSubmissionForm::submitWebformSubmission returns an array
146+ // if the submission is not valid.
147+ if (is_array ($ submission )) {
148+ \Drupal::logger ('os2forms_forloeb ' )->error (
149+ "Can't create new submission: " . json_encode ($ submission )
150+ );
151+ \Drupal::messenger ()->addError ('Webform data is invalid and could not be submitted. ' );
152+ return FALSE ;
153+ }
132154
133- // Submit the webform submission.
134- $ submission = WebformSubmissionForm::submitWebformSubmission ($ new_submission );
155+ $ taskUniqueSubmissionId = $ templateTask ['data ' ]['unique_id ' ];
135156
136- // WebformSubmissionForm::submitWebformSubmission returns an array
137- // if the submission is not valid.
138- if ( is_array ( $ submission )) {
139- \Drupal:: logger ( ' os2forms_forloeb ' )-> error (
140- " Can't create new submission: " . json_encode ( $ submission )
157+ // Attach it to the Maestro process.
158+ $ sid = $ new_submission -> id ();
159+ MaestroEngine:: createEntityIdentifier (
160+ $ this -> processID , $ new_submission -> getEntityTypeId (),
161+ $ new_submission -> bundle (), $ taskUniqueSubmissionId , $ sid
141162 );
142- \Drupal::messenger ()->addError ('Webform data is invalid and could not be submitted. ' );
143- return FALSE ;
144- }
145163
146- // Attach it to the Maestro process.
147- $ sid = $ new_submission ->id ();
148- MaestroEngine::createEntityIdentifier (
149- $ this ->processID , $ new_submission ->getEntityTypeId (),
150- $ new_submission ->bundle (), $ taskUniqueSubmissionId , $ sid
151- );
152-
153- $ form = parent ::getExecutableForm ($ modal , $ parent );
154- // Catch os2forms-forloeb access token and pass it further.
155- if ($ form instanceof RedirectResponse && $ token = \Drupal::request ()->query ->get ('os2forms-forloeb-ws-token ' )) {
156- // Check token to previous submission and update it to new one.
157- if ($ token === $ webform_submission ->getToken ()) {
158- $ token = $ new_submission ->getToken ();
159- $ url = Url::fromUserInput ($ form ->getTargetUrl (), ['query ' => ['os2forms-forloeb-ws-token ' => $ token ]]);
160- $ form = new RedirectResponse ($ url ->toString ());
164+ // Important: Apparently the form must be generated after calling
165+ // MaestroEngine::createEntityIdentifier for this to work.
166+ $ form = parent ::getExecutableForm ($ modal , $ parent );
167+ // Catch os2forms-forloeb access token and pass it further.
168+ if ($ form instanceof RedirectResponse && $ token = \Drupal::request ()->query ->get ('os2forms-forloeb-ws-token ' )) {
169+ // Check token to previous submission and update it to new one.
170+ if ($ token === $ webform_submission ->getToken ()) {
171+ $ token = $ new_submission ->getToken ();
172+ $ url = Url::fromUserInput ($ form ->getTargetUrl (), ['query ' => ['os2forms-forloeb-ws-token ' => $ token ]]);
173+ $ form = new RedirectResponse ($ url ->toString ());
174+ }
161175 }
162176 }
177+ else {
178+ // Store values in session.
179+ $ values ['processID ' ] = $ this ->processID ;
180+ $ values ['queueID ' ] = $ this ->queueID ;
181+ $ values ['webformInheritID ' ] = $ webformInheritID ;
182+
183+ self ::setTaskValues ($ this ->queueID , $ values );
184+
185+ $ form = parent ::getExecutableForm ($ modal , $ parent );
186+ }
163187
164188 return $ form ;
165189 }
166190
191+ /**
192+ * Implements hook_ENTITY_TYPE_prepare_form().
193+ */
194+ public static function webformSubmissionPrepareForm (WebformSubmissionInterface $ webformSubmission , string $ operation , FormStateInterface $ formState ): void {
195+ $ request = \Drupal::request ();
196+ $ isMaestro = (bool ) $ request ->query ->get ('maestro ' , 0 );
197+ $ queueID = (int ) $ request ->query ->get ('queueid ' , 0 );
198+ if ($ isMaestro && $ queueID > 0 ) {
199+ $ values = self ::getTaskValues ($ queueID );
200+ if (isset ($ values ['data ' ])) {
201+ foreach ($ values ['data ' ] as $ name => $ value ) {
202+ $ webformSubmission ->setElementData ($ name , $ value );
203+ }
204+ }
205+ }
206+ }
207+
208+ /**
209+ * Get task values from session.
210+ *
211+ * @param int $queueID
212+ * The queue ID.
213+ *
214+ * @return array
215+ * The task values if any.
216+ */
217+ private static function getTaskValues ($ queueID ) {
218+ $ sessionKey = self ::formatTaskValuesSessionKey ($ queueID );
219+ return \Drupal::request ()->getSession ()->get ($ sessionKey );
220+ }
221+
222+ /**
223+ * Set task values in session.
224+ *
225+ * @param int $queueID
226+ * The queue ID.
227+ * @param array $values
228+ * The values.
229+ */
230+ private static function setTaskValues ($ queueID , array $ values ) {
231+ $ sessionKey = self ::formatTaskValuesSessionKey ($ queueID );
232+ \Drupal::request ()->getSession ()->set ($ sessionKey , $ values );
233+ }
234+
235+ /**
236+ * Format task values session key.
237+ *
238+ * @param int $queueID
239+ * The queue ID.
240+ *
241+ * @return string
242+ * The formatted session key.
243+ */
244+ private static function formatTaskValuesSessionKey ($ queueID ) {
245+ return sprintf ('os2forms_forloeb_inherited_values_%s ' , $ queueID );
246+ }
247+
167248}
0 commit comments