Skip to content

Commit

Permalink
SUPP0RT-1293: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jekuaitk committed Nov 8, 2023
1 parent 4303abd commit 3c4fda0
Showing 1 changed file with 29 additions and 12 deletions.
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->getRecipientElementsOptions();
$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 getRecipientElementsOptions(): array {
$elements = $this->getWebform()->getElementsDecodedAndFlattened();

$elementTypes = [
Expand All @@ -241,6 +241,8 @@ private function getRecipientElements(): array {
'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);
Expand All @@ -252,26 +254,41 @@ private function getRecipientElements(): array {
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;
}
}
}
}

$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 3c4fda0

Please sign in to comment.