From 9724890f77698d0ffd90b5d2a4120c9d291bab83 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Thu, 9 Nov 2023 09:21:12 +0100 Subject: [PATCH] SUPP0RT-1293: Updated os2forms maestro handler patch --- ...ts_in_maestro_notification_recipient.patch | 104 ++++++++++++++---- 1 file changed, 82 insertions(+), 22 deletions(-) diff --git a/patches/drupal/os2forms/allow_composite_elements_in_maestro_notification_recipient.patch b/patches/drupal/os2forms/allow_composite_elements_in_maestro_notification_recipient.patch index b3d6f2cd..da4b8c48 100644 --- a/patches/drupal/os2forms/allow_composite_elements_in_maestro_notification_recipient.patch +++ b/patches/drupal/os2forms/allow_composite_elements_in_maestro_notification_recipient.patch @@ -1,23 +1,27 @@ diff --git a/modules/os2forms_forloeb/src/MaestroHelper.php b/modules/os2forms_forloeb/src/MaestroHelper.php -index ce0b5fb..565ad23 100644 +index ce0b5fb..dfa65ca 100644 --- a/modules/os2forms_forloeb/src/MaestroHelper.php +++ b/modules/os2forms_forloeb/src/MaestroHelper.php -@@ -466,6 +466,21 @@ class MaestroHelper implements LoggerInterface { +@@ -8,6 +8,7 @@ use Drupal\advancedqueue\Entity\QueueInterface; + use Drupal\advancedqueue\Job; + use Drupal\advancedqueue\JobResult; + use Drupal\Component\Render\MarkupInterface; ++use Drupal\Component\Utility\NestedArray; + use Drupal\Core\Config\ConfigFactoryInterface; + use Drupal\Core\Config\ImmutableConfig; + use Drupal\Core\Entity\EntityStorageInterface; +@@ -466,6 +467,17 @@ class MaestroHelper implements LoggerInterface { ?? $data[$recipientElement] ?? NULL; - + + // Handle composite elements. + if ($recipient === NULL) { ++ // Composite subelement keys consist of ++ // the composite element key and the subelement key separated by '__', ++ // e.g. 'contact__name'. + if (str_contains($recipientElement, '__')) { + $keys = explode('__', $recipientElement); -+ $result = $data; -+ foreach ($keys as $key) { -+ if (array_key_exists($key, $result)) { -+ $result = $result[$key]; -+ } -+ } -+ -+ $recipient = $result; ++ $recipient = NestedArray::getValue($data, $keys); + } + } + @@ -25,30 +29,56 @@ index ce0b5fb..565ad23 100644 $recipient = $settings[MaestroNotificationHandler::NOTIFICATION][$notificationType][MaestroNotificationHandler::NOTIFICATION_RECIPIENT] ?? NULL; } diff --git a/modules/os2forms_forloeb/src/Plugin/WebformHandler/MaestroNotificationHandler.php b/modules/os2forms_forloeb/src/Plugin/WebformHandler/MaestroNotificationHandler.php -index d0afc20..f907ba7 100644 +index d0afc20..35c32d8 100644 --- a/modules/os2forms_forloeb/src/Plugin/WebformHandler/MaestroNotificationHandler.php +++ b/modules/os2forms_forloeb/src/Plugin/WebformHandler/MaestroNotificationHandler.php -@@ -240,6 +240,28 @@ final class MaestroNotificationHandler extends WebformHandlerBase { +@@ -99,7 +99,7 @@ final class MaestroNotificationHandler extends WebformHandlerBase { + '#title' => $this->t('Notification'), + ]; + +- $availableElements = $this->getRecipientElements(); ++ $availableElements = $this->getRecipientElementOptions(); + $form[self::NOTIFICATION][static::RECIPIENT_ELEMENT] = [ + '#type' => 'select', + '#title' => $this->t('Element that contains the recipient identifier (email, CPR or CVR) of the notification'), +@@ -228,7 +228,7 @@ final class MaestroNotificationHandler extends WebformHandlerBase { + /** + * Get recipient elements. + */ +- private function getRecipientElements(): array { ++ private function getRecipientElementOptions(): array { + $elements = $this->getWebform()->getElementsDecodedAndFlattened(); + + $elementTypes = [ +@@ -240,16 +240,60 @@ final class MaestroNotificationHandler extends WebformHandlerBase { 'cvr_value_element', 'os2forms_person_lookup', ]; + ++ $isAllowedElement = static fn ($e) => in_array($e['#type'], $elementTypes, TRUE); ++ + // Expand composite elements, NOT custom composite elements. + foreach ($elements as $key => $element) { -+ $orgElement = $this->getWebform()->getElement($key); ++ $formElement = $this->getWebform()->getElement($key); + -+ if (array_key_exists('#webform_composite_elements', $orgElement)) { -+ foreach ($orgElement['#webform_composite_elements'] as $compositeElement) { ++ if ('webform_custom_composite' === $formElement['#type']) { ++ continue; ++ } ++ ++ if (isset($formElement['#webform_composite_elements'])) { ++ foreach ($formElement['#webform_composite_elements'] as $compositeElement) { + // If composite element is not accessible ignore it. -+ if (array_key_exists('#access', $compositeElement) && !$compositeElement['#access']) { ++ if (!($compositeElement['#access'] ?? TRUE)) { + continue; + } + -+ if (in_array($compositeElement['#type'], $elementTypes, TRUE)) { -+ $elements[$compositeElement['#webform_composite_key']] = [ -+ '#title' => (string) $compositeElement['#title'], -+ '#type' => $compositeElement['#type'], ++ if ($isAllowedElement($compositeElement)) { ++ // Group composite subelements. ++ $elements[$element['#title']][$compositeElement['#webform_composite_key']] = [ ++ '#title' => $compositeElement['#title'], + ]; ++ ++ $elements[$element['#title']]['#is_composite'] = TRUE; + } + } + } @@ -56,4 +86,34 @@ index d0afc20..f907ba7 100644 + $elements = array_filter( $elements, - static function (array $element) use ($elementTypes) { +- static function (array $element) use ($elementTypes) { +- return in_array($element['#type'], $elementTypes, TRUE); +- } ++ static fn (array $element) => $isAllowedElement($element) ++ // Composite elements are already filtered, ++ // i.e. they do not need to be filtered here. ++ || ($element['#is_composite'] ?? FALSE) + ); + +- return array_map(static function (array $element) { +- return $element['#title']; +- }, $elements); ++ // Get titles of remaining elements. ++ return array_map( ++ static function (array $element) { ++ if ($element['#is_composite'] ?? FALSE) { ++ ++ return array_map( ++ static fn (array $compositeElement) => $compositeElement['#title'], ++ // Consider only elements with a title, ++ // i.e. the subelements we added earlier. ++ array_filter($element, static fn ($e) => isset($e['#title']))); ++ } ++ ++ return $element['#title']; ++ }, ++ $elements ++ ); + } + + /**