From 991b5616051870f3380f1e22a07d3c90fbe44d14 Mon Sep 17 00:00:00 2001 From: mb Date: Tue, 23 Nov 2021 15:08:41 +0100 Subject: [PATCH 1/2] [#46800] Created new task-type for inheriting webform in flow --- .../EngineTasks/MaestroWebformInheritTask.php | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 src/Plugin/EngineTasks/MaestroWebformInheritTask.php diff --git a/src/Plugin/EngineTasks/MaestroWebformInheritTask.php b/src/Plugin/EngineTasks/MaestroWebformInheritTask.php new file mode 100644 index 0000000..752c387 --- /dev/null +++ b/src/Plugin/EngineTasks/MaestroWebformInheritTask.php @@ -0,0 +1,150 @@ +processID = $configuration[0]; + $this->queueID = $configuration[1]; + } + } + + /** + * {@inheritDoc} + */ + public function shortDescription() { + return t('Webform with Inherited submission'); + } + + /** + * {@inheritDoc} + */ + public function description() { + return $this->t('Webform with Inherited submission'); + } + + /** + * {@inheritDoc} + * + * @see \Drupal\Component\Plugin\PluginBase::getPluginId() + */ + public function getPluginId() { + return 'MaestroWebformInherit'; + } + + /** + * {@inheritDoc} + */ + public function getTaskEditForm(array $task, $templateMachineName) { + + // We call the parent, as we need to add a field to the inherited form + $form = parent::getTaskEditForm($task, $templateMachineName); + $form['inherit_webform_unique_id'] = [ + '#type' => 'textfield', + '#title' => $this->t('Inherit Webform from:'), + '#description' => $this->t('Put the unique identifier of the webform you want to inherit from (start-task=submission'), + '#default_value' => $task['data']['inherit_webform_unique_id'] ?? '', + '#required' => TRUE, + ]; + return $form; + } + + /** + * {@inheritDoc} + */ + public function prepareTaskForSave(array &$form, FormStateInterface $form_state, array &$task) { + + // The field added in getTaskEditForm, needs to be included here, otherwise it won't be saved. + $task['data']['inherit_webform_unique_id'] = $form_state->getValue('inherit_webform_unique_id'); + } + + /** + * {@inheritDoc} + */ + public function getExecutableForm($modal, MaestroExecuteInteractive $parent) { + + // First, get hold of the interesting previous tasks. + $templateMachineName = MaestroEngine::getTemplateIdFromProcessId($this->processID); + $taskMachineName = MaestroEngine::getTaskIdFromQueueId($this->queueID); + $task = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskMachineName); + + // Get user input from 'inherit_webform_unique_id' + $webformInheritID = $task['data']['inherit_webform_unique_id']; + + // Load its corresponding webform submission. + $sid = MaestroEngine::getEntityIdentiferByUniqueID($this->processID, $webformInheritID); + if ($sid) { + $webform_submission = WebformSubmission::load($sid); + } + if (!isset($webform_submission)) { + \Drupal::logger('os2forms_forloeb')->error( + "Predecessors must have submissions with webforms attached." + ); + } + // Copy the fields of the webform submission to the values array. + foreach ($webform_submission->getData() as $key => $value) { + if ($value) { + $field_values[$webformInheritID . '_' . $key] = $value; + } + } + // Now create webform submission, submit and attach to current process. + $templateTask = MaestroEngine::getTemplateTaskByQueueID($this->queueID); + $taskUniqueSubmissionId = $templateTask['data']['unique_id']; + $webformMachineName = $templateTask['data']['webform_machine_name']; + + $values = []; + $values['webform_id'] = $webformMachineName; + $values['data'] = $field_values; + + // Create submission. + $new_submission = WebformSubmission::create($values); + + $errors = WebformSubmissionForm::validateWebformSubmission($webform_submission); + + if (!empty($errors)) { + \Drupal::logger('os2forms_forloeb')->error( + "Can't create new submission: " . json_encode($errors) + ); + } + // Submit it. + $new_submission = WebformSubmissionForm::submitWebformSubmission($new_submission); + + // Attach it to the Maestro process. + $sid = $new_submission->id(); + MaestroEngine::createEntityIdentifier( + $this->processID, $new_submission->getEntityTypeId(), + $new_submission->bundle(), $taskUniqueSubmissionId, $sid + ); + + return parent::getExecutableForm($modal, $parent); + } +} From 9b0d930fc769e2d2c87743f47cbe3518db8d50e2 Mon Sep 17 00:00:00 2001 From: mb Date: Fri, 26 Nov 2021 10:49:39 +0100 Subject: [PATCH 2/2] [#46800] adding returns after logs --- src/Plugin/EngineTasks/MaestroWebformInheritTask.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Plugin/EngineTasks/MaestroWebformInheritTask.php b/src/Plugin/EngineTasks/MaestroWebformInheritTask.php index 752c387..289c06d 100644 --- a/src/Plugin/EngineTasks/MaestroWebformInheritTask.php +++ b/src/Plugin/EngineTasks/MaestroWebformInheritTask.php @@ -109,6 +109,7 @@ public function getExecutableForm($modal, MaestroExecuteInteractive $parent) { \Drupal::logger('os2forms_forloeb')->error( "Predecessors must have submissions with webforms attached." ); + return FALSE; } // Copy the fields of the webform submission to the values array. foreach ($webform_submission->getData() as $key => $value) { @@ -134,6 +135,7 @@ public function getExecutableForm($modal, MaestroExecuteInteractive $parent) { \Drupal::logger('os2forms_forloeb')->error( "Can't create new submission: " . json_encode($errors) ); + return FALSE; } // Submit it. $new_submission = WebformSubmissionForm::submitWebformSubmission($new_submission);