mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 23:18:10 +00:00
fix(#123): normalizes non-named select config options for recipes
This commit is contained in:
parent
dac8064938
commit
7c8e55588a
2 changed files with 88 additions and 0 deletions
|
|
@ -80,6 +80,9 @@ class PluginImportService
|
|||
$settings['custom_fields'] = [];
|
||||
}
|
||||
|
||||
// Normalize options in custom_fields (convert non-named values to named values)
|
||||
$settings['custom_fields'] = $this->normalizeCustomFieldsOptions($settings['custom_fields']);
|
||||
|
||||
// Create configuration template with the custom fields
|
||||
$configurationTemplate = [
|
||||
'custom_fields' => $settings['custom_fields'],
|
||||
|
|
@ -206,6 +209,9 @@ class PluginImportService
|
|||
$settings['custom_fields'] = [];
|
||||
}
|
||||
|
||||
// Normalize options in custom_fields (convert non-named values to named values)
|
||||
$settings['custom_fields'] = $this->normalizeCustomFieldsOptions($settings['custom_fields']);
|
||||
|
||||
// Create configuration template with the custom fields
|
||||
$configurationTemplate = [
|
||||
'custom_fields' => $settings['custom_fields'],
|
||||
|
|
@ -385,6 +391,49 @@ class PluginImportService
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize options in custom_fields by converting non-named values to named values
|
||||
* This ensures that options like ["true", "false"] become [["true" => "true"], ["false" => "false"]]
|
||||
*
|
||||
* @param array $customFields The custom_fields array from settings
|
||||
* @return array The normalized custom_fields array
|
||||
*/
|
||||
private function normalizeCustomFieldsOptions(array $customFields): array
|
||||
{
|
||||
foreach ($customFields as &$field) {
|
||||
// Only process select fields with options
|
||||
if (isset($field['field_type']) && $field['field_type'] === 'select' && isset($field['options']) && is_array($field['options'])) {
|
||||
$normalizedOptions = [];
|
||||
foreach ($field['options'] as $option) {
|
||||
// If option is already a named value (array with key-value pair), keep it as is
|
||||
if (is_array($option)) {
|
||||
$normalizedOptions[] = $option;
|
||||
} else {
|
||||
// Convert non-named value to named value
|
||||
// Convert boolean to string, use lowercase for label
|
||||
$value = is_bool($option) ? ($option ? 'true' : 'false') : (string) $option;
|
||||
$normalizedOptions[] = [$value => $value];
|
||||
}
|
||||
}
|
||||
$field['options'] = $normalizedOptions;
|
||||
|
||||
// Normalize default value to match normalized option values
|
||||
if (isset($field['default'])) {
|
||||
$default = $field['default'];
|
||||
// If default is boolean, convert to string to match normalized options
|
||||
if (is_bool($default)) {
|
||||
$field['default'] = $default ? 'true' : 'false';
|
||||
} else {
|
||||
// Convert to string to ensure consistency
|
||||
$field['default'] = (string) $default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $customFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that template and context are within command-line argument limits
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue