Skip to content

Commit 60a95f1

Browse files
authored
Merge pull request #95 from systopia/use-custom-options-for-item-display
Use custom options to specify how array items are displayed
2 parents 30b4a16 + 3546613 commit 60a95f1

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,35 @@ In this implementation there are some custom features and keywords not
99
specified in standard JSON Forms. (TODO: Not all additional possibilities are
1010
described here, yet.)
1111

12+
### Array control: options `itemLayout` and `elements`
13+
14+
There are two additional options in controls referencing an array.
15+
16+
The option `itemLayout` allows to specify the layout for the individual controls
17+
of an array item (e.g. `VerticalLayout`). The default is `TableRow`.
18+
19+
The option `elements` allows to specify the controls for the properties of an
20+
array item. By default a control is generated for each item property.
21+
22+
Example:
23+
```json
24+
{
25+
"itemLayout": "VerticalLayout",
26+
"elements": [
27+
{
28+
"type": "Control",
29+
"label": "Name",
30+
"scope": "#/properties/name"
31+
},
32+
{
33+
"type": "Control",
34+
"label": "Birth Date",
35+
"scope": "#/properties/birthDate"
36+
}
37+
]
38+
}
39+
```
40+
1241
### Description display
1342

1443
The Keyword `descriptionDisplay` in Control options allows to specify the
@@ -39,4 +68,7 @@ Some things cannot be done with (standard) Drupal forms, e.g.
3968
[Rules](https://jsonforms.io/docs/uischema/rules/) cannot completely be mapped
4069
to [conditional form fields](https://www.drupal.org/docs/drupal-apis/form-api/conditional-form-fields).
4170

71+
The [`detail` option](https://jsonforms.io/docs/uischema/controls#the-detail-option)
72+
for controls referencing an array is unsupported.
73+
4274
TODO: Describe all limitations.

src/Form/Control/ArrayArrayFactory.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,19 +209,34 @@ private function buildTableHeader(LayoutDefinition $arrayLayoutDefinition): arra
209209
}
210210

211211
private function createLayoutDefinition(ArrayControlDefinition $definition): ArrayLayoutDefinition {
212-
// Note: We actually do not use "detail" as it is described in JSON Forms:
212+
// Note: We used to use "detail" as specification of the "normal" display,
213+
// not as it is described in JSON Forms:
213214
// https://jsonforms.io/docs/uischema/controls#the-detail-option
214-
// Should we use another option name instead?
215-
$arrayUiSchema = $definition->getOptionsValue('detail');
216-
if (!$arrayUiSchema instanceof \stdClass) {
217-
$arrayUiSchema = new \stdClass();
215+
// Now we use the option "elements" to specify individual controls for the
216+
// properties of an item and the option "itemLayout" to specify the layout
217+
// to display the elements.
218+
$detail = $definition->getOptionsValue('detail');
219+
Assertion::nullOrIsInstanceOf($detail, \stdClass::class);
220+
if (isset($detail->type) || isset($detail->elements)) {
221+
// phpcs:disable Drupal.Semantics.FunctionTriggerError.TriggerErrorTextLayoutRelaxed
222+
@trigger_error(<<<EOD
223+
The properties "type" and "elements" in option "detail" of an array
224+
control are deprecated and were never supported in accordance to the
225+
JSON Forms documentation. Use the options "itemLayout" and "elements"
226+
instead.
227+
EOD,
228+
E_USER_DEPRECATED
229+
);
230+
// phpcs:enable
218231
}
219232

220-
$arrayUiSchema->type ??= 'TableRow';
233+
$arrayUiSchema = new \stdClass();
234+
$arrayUiSchema->type = $definition->getOptionsValue('itemLayout') ?? $detail->type ?? 'TableRow';
221235

222236
$items = $definition->getItems();
223237
Assertion::isInstanceOf($items, \stdClass::class);
224-
$arrayUiSchema->elements ??= $this->createElementSchemas($items);
238+
$arrayUiSchema->elements = $definition->getOptionsValue('elements') ?? $detail->elements
239+
?? $this->createElementSchemas($items);
225240

226241
return new ArrayLayoutDefinition(
227242
$arrayUiSchema, $items, $definition->isUiReadonly(), $definition->getRootDefinition()

0 commit comments

Comments
 (0)