Open rasteiner opened 1 year ago
... or the whole "defaults and query parsing" steps of each item could be completely sidestepped for associative arrays when no text
and value
is present in the options.
I heard you're going to drop php 8.0 support for Kirby 4, so you could use the new array_is_list
function.
// convert result to a collection, or directly consume associative array
if (is_array($result) === true) {
if(array_is_list($result) === false && $this->text === null && $this->value === null) {
return Options::factory(array_map(
fn($text, $value) => compact('text', 'value'),
$result,
array_keys($result)
));
} else {
$result = $this->collection($result);
}
}
Description
It is unclear how to use an associative array of scalar values as query result for panel fields which accept options. Just using an associative array stores the "values" (the text which is presented to the user as "value" in the content file).
Expected behavior
Associative arrays of scalars should just be accepted as is and store the array
key
as value in the content file.should be doable like:
Otherwise, the docs should mention how to use associative arrays.
To reproduce
Additional context
The scalar values of the associative array are transformed to a
Kirby\Toolkit\Obj
, with fieldskey
andvalue
. This allows us to use an associative array by explicitly telling kirby to use "text"{{ item.value }}
and "value"{{ item.key }}
, I didn't find any docs mentioning this however.Contrary to OptionsApi, OptionsQuery doesn't have a default config for
key
andvalue
, see https://github.com/getkirby/kirby/blob/4.0.0-beta.2/src/Option/OptionsQuery.php#L68However, just changing the "array default" from "value, value" to "value, key" would break non associative arrays (it would start storing the numeric key). To work around this, the
collection
method could storestring
keys separately in theObj
and then a new default which looks for that: