|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2forms_maestro_webform\Plugin\WebformHandler; |
| 4 | + |
| 5 | +use Drupal\Core\Form\FormStateInterface; |
| 6 | +use Drupal\Core\Serialization\Yaml; |
| 7 | +use Drupal\maestro\Engine\MaestroEngine; |
| 8 | +use Drupal\os2forms_digital_post\Helper\WebformHelperSF1601; |
| 9 | +use Drupal\os2forms_maestro_webform\MaestroHelper; |
| 10 | +use Drupal\webform\Plugin\WebformHandlerBase; |
| 11 | +use Drupal\webform\WebformInterface; |
| 12 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Maestro notification handler. |
| 16 | + * |
| 17 | + * @WebformHandler( |
| 18 | + * id = "os2forms_maestro_webform_workflow_submission_element", |
| 19 | + * label = @Translation("Maestro workflow submission element"), |
| 20 | + * category = @Translation("Web services"), |
| 21 | + * description = @Translation("Prefills form elements with values from workflow submissions."), |
| 22 | + * cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED, |
| 23 | + * results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_IGNORED, |
| 24 | + * submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED, |
| 25 | + * ) |
| 26 | + */ |
| 27 | +final class WorkflowSubmissionElementValues extends WebformHandlerBase { |
| 28 | + public const SPEC = 'spec'; |
| 29 | + |
| 30 | + /** |
| 31 | + * {@inheritdoc} |
| 32 | + */ |
| 33 | + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
| 34 | + $instance = new static($configuration, $plugin_id, $plugin_definition); |
| 35 | + |
| 36 | + $instance->loggerFactory = $container->get('logger.factory'); |
| 37 | + $instance->configFactory = $container->get('config.factory'); |
| 38 | + $instance->renderer = $container->get('renderer'); |
| 39 | + $instance->entityTypeManager = $container->get('entity_type.manager'); |
| 40 | + $instance->conditionsValidator = $container->get('webform_submission.conditions_validator'); |
| 41 | + $instance->tokenManager = $container->get('webform.token_manager'); |
| 42 | + |
| 43 | + $instance->setConfiguration($configuration); |
| 44 | + |
| 45 | + return $instance; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritdoc} |
| 50 | + */ |
| 51 | + public function alterElements(array &$elements, WebformInterface $webform) { |
| 52 | + $spec = $this->getSpec(); |
| 53 | + |
| 54 | + $submissionValues = []; |
| 55 | + if ($queueID = \Drupal::request()->query->get('queueid')) { |
| 56 | + if ($processID = (MaestroEngine::getProcessIdFromQueueId($queueID) ?: NULL)) { |
| 57 | + $entityIdentifiers = MaestroHelper::getWebformSubmissionIdentifiersForProcess($processID); |
| 58 | + foreach ($entityIdentifiers as $entityIdentifier) { |
| 59 | + /** @var \Drupal\webform\WebformSubmissionInterface $submission */ |
| 60 | + $submission = $this->submissionStorage->load($entityIdentifier['entity_id']); |
| 61 | + $webform = $submission->getWebform(); |
| 62 | + $submissionValues[$webform->id()] = $submission->getData(); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + foreach ($spec as $key => $info) { |
| 68 | + if (isset($elements[$key])) { |
| 69 | + if (isset($info['form'], $info['element']) |
| 70 | + && isset($submissionValues[$info['form']]) |
| 71 | + && isset($submissionValues[$info['form']][$info['element']])) { |
| 72 | + $elements[$key]['#default_value'] = $submissionValues[$info['form']][$info['element']]; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private function getSpec(): array { |
| 79 | + $spec = []; |
| 80 | + try { |
| 81 | + $spec = Yaml::decode($this->configuration[self::SPEC]); |
| 82 | + } catch (\Throwable) {} |
| 83 | + |
| 84 | + return is_array($spec) ? $spec : []; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * {@inheritdoc} |
| 89 | + */ |
| 90 | + public function getSummary() { |
| 91 | + return [ |
| 92 | + '#markup' => $this->t('Fill elements with flow values: <pre>%spec</pre>', ['%spec' => Yaml::encode($this->getSpec())]), |
| 93 | + ]; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * {@inheritdoc} |
| 98 | + */ |
| 99 | + public function buildConfigurationForm(array $form, FormStateInterface $formState) { |
| 100 | + $form[self::SPEC] = [ |
| 101 | + '#type' => 'textarea', |
| 102 | + '#title' => $this->t('Spec'), |
| 103 | + '#description' => $this->t('Default value on elements will override this'), |
| 104 | + '#required' => TRUE, |
| 105 | + '#default_value' => $this->configuration[self::SPEC] ?? NULL, |
| 106 | + ]; |
| 107 | + |
| 108 | + return $this->setSettingsParents($form); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * {@inheritdoc} |
| 113 | + */ |
| 114 | + public function submitConfigurationForm(array &$form, FormStateInterface $formState) { |
| 115 | + parent::submitConfigurationForm($form, $formState); |
| 116 | + |
| 117 | + $this->configuration[self::SPEC] = $formState->getValue(self::SPEC); |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments