Skip to content
Snippets Groups Projects
Commit a695747c authored by Sviatoslav Smovdyr's avatar Sviatoslav Smovdyr
Browse files

Merge branch '3483496-2.0.0-beta5-preselect-option' into '2.0.x'

#3483496 - Made autoselect for a single option in the select. Simplified the...

See merge request !260
parents e6a1e80d b32f40c6
Branches
Tags
No related merge requests found
Pipeline #348642 passed
......@@ -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;
}
......
......@@ -46,7 +46,7 @@ class EntityReferencedSource extends DerivableContextSourceBase {
$returned = [];
foreach ($options as $option_key => $option_value) {
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) {
......@@ -55,6 +55,7 @@ class EntityReferencedSource extends DerivableContextSourceBase {
foreach ($grouped_option_keys as $grouped_option_key) {
if ($grouped_option_key !== $group_key) {
$label = (string) $options[$grouped_option_key];
// @phpstan-ignore-next-line
$returned[$group][$grouped_option_key] = explode(" referenced by ", $label)[0];
}
}
......@@ -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