Skip to content
Snippets Groups Projects
Commit 71517119 authored by Sviatoslav Smovdyr's avatar Sviatoslav Smovdyr Committed by Mikael Meulle
Browse files

Issue #3483496 by smovs, just_like_good_vibes, pdureau: Preselect option when only one available

parent e6a1e80d
No related branches found
No related tags found
1 merge request!260#3483496 - Made autoselect for a single option in the select. Simplified the...
Pipeline #348659 passed
...@@ -278,6 +278,10 @@ abstract class DerivableContextSourceBase extends SourcePluginBase { ...@@ -278,6 +278,10 @@ abstract class DerivableContextSourceBase extends SourcePluginBase {
} }
} }
} }
// Set default value to the only available option.
if (!$derivable_context && count($options_derivable_contexts) === 1) {
$derivable_context = array_key_first($options_derivable_contexts);
}
return $derivable_context; return $derivable_context;
} }
......
...@@ -46,7 +46,7 @@ class EntityReferencedSource extends DerivableContextSourceBase { ...@@ -46,7 +46,7 @@ class EntityReferencedSource extends DerivableContextSourceBase {
$returned = []; $returned = [];
foreach ($options as $option_key => $option_value) { foreach ($options as $option_key => $option_value) {
if (!isset($option_to_group[$option_key])) { if (!isset($option_to_group[$option_key])) {
$returned[$option_key] = $option_value; $returned[$option_key] = (string) $this->getSimplifiedOptionWording($option_value);
} }
} }
foreach ($groups as $group_key => $grouped_option_keys) { foreach ($groups as $group_key => $grouped_option_keys) {
...@@ -55,6 +55,7 @@ class EntityReferencedSource extends DerivableContextSourceBase { ...@@ -55,6 +55,7 @@ class EntityReferencedSource extends DerivableContextSourceBase {
foreach ($grouped_option_keys as $grouped_option_key) { foreach ($grouped_option_keys as $grouped_option_key) {
if ($grouped_option_key !== $group_key) { if ($grouped_option_key !== $group_key) {
$label = (string) $options[$grouped_option_key]; $label = (string) $options[$grouped_option_key];
// @phpstan-ignore-next-line
$returned[$group][$grouped_option_key] = explode(" referenced by ", $label)[0]; $returned[$group][$grouped_option_key] = explode(" referenced by ", $label)[0];
} }
} }
...@@ -111,4 +112,37 @@ class EntityReferencedSource extends DerivableContextSourceBase { ...@@ -111,4 +112,37 @@ class EntityReferencedSource extends DerivableContextSourceBase {
]; ];
} }
/**
* Return simplified wording for an option.
*
* @param mixed $option_value
* The option value.
*
* @return mixed
* The simplified option.
*/
protected function getSimplifiedOptionWording(mixed $option_value): mixed {
if (!($option_value instanceof TranslatableMarkup)) {
return $option_value;
}
$arguments = $option_value->getArguments();
// Modify @bundle to keep only the part before any parentheses.
if (isset($arguments['@bundle'])) {
$bundle_value = $arguments['@bundle'];
// Keep only the part before parentheses, if present.
if (is_string($bundle_value) &&
preg_match('/^(.*?)\s*\(/', $bundle_value, $matches)) {
$arguments['@bundle'] = trim($matches[1]);
}
}
return new TranslatableMarkup(
'@field (@bundle)',
$arguments,
$option_value->getOptions()
);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment