Skip to content

Commit

Permalink
Merge pull request #74 from OS2Forms/feature/SUPP0RT-1293-allow-compo…
Browse files Browse the repository at this point in the history
…site-elements-in-maestro-notification-recipient

SUPP0RT-1293: Allowed composite elements in Maestro notification reci…
  • Loading branch information
jekuaitk authored Dec 5, 2023
2 parents 344e816 + a19b7b6 commit 2c47532
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa

## [Unreleased]

- [#74](https://github.com/OS2Forms/os2forms/pull/74)
Allow composite elements in Maestro notification recipient
- [#73](https://github.com/OS2Forms/os2forms/pull/73a)
Fix issue with nested elements in webform inherit
- [#77](https://github.com/OS2Forms/os2forms/pull/77)
Expand Down
12 changes: 12 additions & 0 deletions modules/os2forms_forloeb/src/MaestroHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand Down Expand Up @@ -466,6 +467,17 @@ public function renderNotification(WebformSubmissionInterface $submission, strin
?? $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);
$recipient = NestedArray::getValue($data, $keys);
}
}

if ($notificationType === self::NOTIFICATION_ESCALATION) {
$recipient = $settings[MaestroNotificationHandler::NOTIFICATION][$notificationType][MaestroNotificationHandler::NOTIFICATION_RECIPIENT] ?? NULL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $formStat
'#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'),
Expand Down Expand Up @@ -228,7 +228,7 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
/**
* Get recipient elements.
*/
private function getRecipientElements(): array {
private function getRecipientElementOptions(): array {
$elements = $this->getWebform()->getElementsDecodedAndFlattened();

$elementTypes = [
Expand All @@ -240,16 +240,60 @@ private function getRecipientElements(): array {
'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) {
$formElement = $this->getWebform()->getElement($key);

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 (!($compositeElement['#access'] ?? TRUE)) {
continue;
}

if ($isAllowedElement($compositeElement)) {
// Group composite subelements.
$elements[$element['#title']][$compositeElement['#webform_composite_key']] = [
'#title' => $compositeElement['#title'],
];

$elements[$element['#title']]['#is_composite'] = TRUE;
}
}
}
}

$elements = array_filter(
$elements,
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
);
}

/**
Expand Down

0 comments on commit 2c47532

Please sign in to comment.