Skip to content

Commit b8429c2

Browse files
committed
Color field: docs for dynamic options
1 parent 17e9b3e commit b8429c2

File tree

1 file changed

+131
-22
lines changed

1 file changed

+131
-22
lines changed

content/docs/3_reference/3_panel/3_fields/0_color/reference-article.txt

+131-22
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ Intro: The `color` field supports multiple color notations, color picker, pre-de
1212

1313
Text:
1414

15+
(image: color.png)
16+
17+
## Field properties
18+
19+
(field-options: color)
20+
1521
## Usage
1622

1723
```yaml
@@ -20,9 +26,39 @@ color:
2026
format: hsl # supports hex (default), rgb, hsl
2127
```
2228

23-
(image: color.png)
2429

25-
## Pre-defined options
30+
### In templates/snippets
31+
32+
```php
33+
<?= $page->color()->escape('css') ?>
34+
```
35+
36+
## Alpha
37+
38+
Use the `alpha` option (default: false) to activate alpha transparency support:
39+
40+
```yaml
41+
color:
42+
type: color
43+
alpha: true
44+
```
45+
46+
## Modes
47+
48+
With the `mode` option you control which elements of the color field are available. Possible values:
49+
50+
| Option | Description |
51+
| ------ | ----------- |
52+
|`picker` | show everything (default)|
53+
|`input` | show only the input |
54+
|`options` | show only options |
55+
56+
(image: color-field-modes.png)
57+
58+
59+
## Options
60+
61+
### Pre-defined options
2662

2763
Allows adding a list of pre-defined colors that are shown as one-click options in the Panel.
2864

@@ -46,39 +82,112 @@ If you name the colors, their name is shown in the Panel as well.
4682
color:
4783
type: color
4884
options:
49-
"Sunny rays": "#F8B195"
50-
"First-love blush": "#F67280"
51-
"Cherry blossom": "#C06C84"
52-
"Morning gloom": "#6C5B7B"
53-
"Midnight rain": "#355C7D"
85+
"#F8B195": "Sunny rays"
86+
"#F67280": "First-love blush"
87+
"#C06C84": "Cherry blossom"
88+
"#6C5B7B": "Morning gloom"
89+
"#355C7D": "Midnight rain"
5490
```
5591

5692
(image: color-field-names.png)
5793

58-
## Modes
94+
<since v="4.1.0">
95+
### Dynamic options
5996

60-
With the `mode` option you control which elements of the color field are available. Possible values:
97+
Our options (link: docs/guide/blueprints/query-language text: query syntax) offers a very powerful way of to add an automatically generated option lists to a color field.
6198

62-
| Option | Description |
63-
| ------ | ----------- |
64-
|`picker` | show everything (default)|
65-
|`input` | show only the input |
66-
|`options` | show only options |
6799

68-
(image: color-field-modes.png)
100+
```yaml
101+
myColorField:
102+
type: color
103+
options:
104+
type: query
105+
query: kirby.option('my.colors')
106+
```
69107

70-
## Alpha
108+
The example above will get the color options from the `my.colors` config entry, which could look like:
109+
110+
```php "site/config/config.php"
111+
// only values
112+
return [
113+
'my' => [
114+
'colors' => [
115+
'#3e3e3e',
116+
'#aaa',
117+
'#ddd',
118+
]
119+
]
120+
];
121+
122+
// values -> labels
123+
return [
124+
'my' => [
125+
'colors' => [
126+
'#3e3e3e' => 'Color A' ,
127+
'#aaa' => 'Color B',
128+
'#ddd' => 'Color C',
129+
]
130+
]
131+
];
132+
```
71133

72-
Use the `alpha` option (default: false) to activate alpha transparency support:
134+
You can start at the `site`, current `page`, `users` collection or the `kirby` instance to run your query.
135+
136+
#### Custom value and label
137+
138+
To customize the stored value and displayed label, you can be more specific when defining the query: `text` and `value` can be defined with the help of our string template language to get exactly what you want as the result.
73139

74140
```yaml
75-
color:
141+
myColorField:
76142
type: color
77-
alpha: true
143+
options:
144+
type: query
145+
query: kirby.option('my.colors')
146+
text: "{{ item.name }}"
147+
value: "{{ item.hex }}"
78148
```
79149

80-
## Use in templates/snippets
150+
```php "site/config/config.php"
151+
return [
152+
'my' => [
153+
'colors' => [
154+
[
155+
'name' => 'Color A',
156+
'hex => '#3e3e3e',
157+
],
158+
[
159+
'name' => 'Color B',
160+
'hex => '#aaa',
161+
],
162+
[
163+
'name' => 'Color C',
164+
'hex => '#ddd',
165+
]
166+
]
167+
]
168+
];
169+
```
81170

82-
```php
83-
<?= $page->color()->escape('css') ?>
171+
### Options via API
172+
173+
If the option queries are not enough or you need to pluck an external source for option data, you can use the API type.
174+
175+
```yaml
176+
myColorField:
177+
type: color
178+
options:
179+
type: api
180+
url: https://your-options-api.com/colors.json
84181
```
182+
183+
By default, the API type expects that the JSON endpoint returns an option array like this:
184+
185+
```js
186+
{
187+
"#3e3e3e": "Color A" ,
188+
"#aaa": "Color B",
189+
"#ddd":"Color C"
190+
}
191+
```
192+
</since>
193+

0 commit comments

Comments
 (0)