This repository was archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
[#46800] Created new task-type for inheriting webform in flow #92
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| <?php | ||
|
|
||
| namespace Drupal\os2forms_forloeb\Plugin\EngineTasks; | ||
|
|
||
| use Drupal\webform\Entity\WebformSubmission; | ||
| use Drupal\webform\WebformSubmissionForm; | ||
| use Drupal\maestro_webform\Plugin\EngineTasks\MaestroWebformTask; | ||
| use Drupal\maestro\Form\MaestroExecuteInteractive; | ||
| use Drupal\maestro\Engine\MaestroEngine; | ||
| use Drupal\Core\Form\FormStateInterface; | ||
| use Drupal\webform\Entity\Webform; | ||
| use Drupal\Core\Ajax\AjaxResponse; | ||
| use Drupal\Core\Ajax\ReplaceCommand; | ||
|
|
||
| /** | ||
| * Maestro Webform Task Plugin for Multiple Submissions. | ||
| * | ||
| * @Plugin( | ||
| * id = "MaestroWebformInherit", | ||
| * task_description = @Translation("Maestro Webform task for multiple submissions."), | ||
| * ) | ||
| */ | ||
| class MaestroWebformInheritTask extends MaestroWebformTask { | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param array $configuration | ||
| * The incoming configuration information from the engine execution. | ||
| * [0] - is the process ID | ||
| * [1] - is the queue ID | ||
| * The processID and queueID properties are defined in the MaestroTaskTrait. | ||
| */ | ||
| public function __construct(array $configuration = NULL) { | ||
| if (is_array($configuration)) { | ||
| $this->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." | ||
| ); | ||
| return FALSE; | ||
| } | ||
| // 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) | ||
| ); | ||
| return FALSE; | ||
| } | ||
| // 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); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.