Skip to content

Commit

Permalink
Color field: warning for deprecated notation
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Dec 7, 2024
1 parent 4b8e371 commit 87076fa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 54 deletions.
40 changes: 22 additions & 18 deletions config/fields/color.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Kirby\Cms\Helpers;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Field\FieldOptions;
use Kirby\Toolkit\A;
Expand Down Expand Up @@ -73,30 +74,33 @@
return [];
}

$options = match (true) {
// simple array of values
// or value=text (from Options class)
if (
is_numeric($options[0]['value']) ||
$options[0]['value'] === $options[0]['text']
=> A::map($options, fn ($option) => [
'value' => $option['text']
]),
) {
// simple array of values
// or value=text (from Options class)
$options = A::map($options, fn ($option) => [
'value' => $option['text']
]);

} elseif ($this->isColor($options[0]['text'])) {
// @deprecated 4.0.0
// TODO: Remove in Kirby 6

// deprecated: name => value, flipping
// TODO: start throwing in warning in v5
$this->isColor($options[0]['text'])
=> A::map($options, fn ($option) => [
'value' => $option['text'],
// ensure that any HTML in the new text is escaped
'text' => Escape::html($option['value'])
]),
Helpers::deprecated('Color field: the text => value notation for options has been deprecated and will be removed in Kirby 6. Please rewrite your options as value => text.');

default
=> A::map($options, fn ($option) => [
$options = A::map($options, fn ($option) => [
'value' => $option['text'],
// ensure that any HTML in the new text is escaped
'text' => Escape::html($option['value'])
]);
} else {
$options = A::map($options, fn ($option) => [
'value' => $option['value'],
'text' => $option['text']
]),
};
]);
}

return $options;
}
Expand Down
36 changes: 0 additions & 36 deletions tests/Form/Fields/ColorFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,6 @@ public function testOptions()
['value' => '#bbb', 'text' => 'Color b'],
['value' => '#ccc', 'text' => 'Color c']
], $field->options());

// Deprecated: name => value
$field = $this->field('color', [
'options' => [
'Color a' => '#aaa',
'Color b' => '#bbb',
'Color c' => '#ccc'
],
]);

$this->assertSame([
['value' => '#aaa', 'text' => 'Color a'],
['value' => '#bbb', 'text' => 'Color b'],
['value' => '#ccc', 'text' => 'Color c']
], $field->options());
}

public function testOptionsFromQuery()
Expand Down Expand Up @@ -134,26 +119,5 @@ public function testOptionsFromQuery()
['value' => '#bbb', 'text' => 'Color b'],
['value' => '#ccc', 'text' => 'Color c']
], $field->options());

// Deprecated: name => value
$this->app = new App([
'options' => [
'foo' => [
'Color a' => '#aaa',
'Color b' => '#bbb',
'Color c' => '#ccc'
],
]
]);

$field = $this->field('color', [
'options' => ['type' => 'query', 'query' => 'kirby.option("foo")'],
]);

$this->assertSame([
['value' => '#aaa', 'text' => 'Color a'],
['value' => '#bbb', 'text' => 'Color b'],
['value' => '#ccc', 'text' => 'Color c']
], $field->options());
}
}

0 comments on commit 87076fa

Please sign in to comment.