-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBasicFormPropertiesFactory.php
More file actions
152 lines (130 loc) · 5.24 KB
/
BasicFormPropertiesFactory.php
File metadata and controls
152 lines (130 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/*
* Copyright (C) 2022 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Drupal\json_forms\Form\Control\Util;
use Drupal\Core\Form\FormStateInterface;
use Drupal\json_forms\Form\Control\Callbacks\RecalculateCallback;
use Drupal\json_forms\Form\Control\Rule\StatesArrayFactory;
use Drupal\json_forms\Form\Util\DescriptionDisplayUtil;
use Drupal\json_forms\JsonForms\Definition\Control\ControlDefinition;
final class BasicFormPropertiesFactory {
/**
* Creates attributes for properties of elements in Drupal form for a Control.
*
* The properties are available in FormElement, and RenderElement used for
* layouts.
*
* @phpstan-return array<string, mixed>
*/
public static function createBasicProperties(ControlDefinition $definition): array {
$form = [
'#parents' => $definition->getPropertyFormParents(),
'#title' => $definition->getLabel(),
'#tree' => TRUE,
'#_scope' => $definition->getFullScope(),
];
if (NULL !== $definition->getDescription()) {
$form['#description'] = $definition->getDescription();
}
// @phpstan-ignore argument.type
DescriptionDisplayUtil::handleDescriptionDisplay($form, $definition->getOptionsValue('descriptionDisplay'));
if (NULL !== $definition->getOptionsValue('placeholder')) {
// @phpstan-ignore offsetAccess.nonOffsetAccessible
$form['#attributes']['placeholder'] = $definition->getOptionsValue('placeholder');
}
if (NULL !== $definition->getRule()) {
$statesArrayFactory = new StatesArrayFactory();
$form['#states'] = $statesArrayFactory->createStatesArray($definition);
}
// Custom option to hide labels, so they are not shown in the form by
// default, but can be used in validation errors.
if (TRUE === $definition->getOptionsValue('hideLabel')) {
$form['#title_display'] = 'invisible';
}
return $form;
}
/**
* Creates attributes for properties in Drupal FormElement for a Control.
*
* @phpstan-return array<string, mixed>
*
* phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
*/
public static function createFieldProperties(ControlDefinition $definition, FormStateInterface $formState): array {
// phpcs:enable
$form = [
'#disabled' => $definition->isReadOnly(),
'#required' => $definition->isRequired(),
'#limit_validation_errors' => [],
'#_nullable' => $definition->isNullable(),
];
$calcInitField = FALSE;
if ($definition->isCalculated()) {
$formState->set('$calculateUsed', TRUE);
}
elseif (TRUE !== $formState->get('$hasCalcInitField')
// Change event cannot be triggered on hidden fields.
&& 'hidden' !== $definition->getOptionsValue('type')
// A field of type managed_field might be used which is rendered in a way
// that it cannot be used as field for initial calculation.
&& 'file' !== $definition->getControlFormat()
) {
$formState->set('$hasCalcInitField', TRUE);
$calcInitField = TRUE;
}
$readOnlyValuePath = array_merge(['readOnlyValues'], $definition->getPropertyPath());
if ((!$formState->isCached() || $definition->isReadOnly())
&& $formState->hasTemporaryValue($definition->getPropertyPath())
) {
$form['#default_value'] = $formState->getTemporaryValue($definition->getPropertyPath());
if ($definition->isReadOnly() && !$definition->isCalculated()) {
// Ensure read only values don't get lost on submit.
$formState->set($readOnlyValuePath, $form['#default_value']);
}
}
elseif ($definition->isReadOnly() && $formState->has($readOnlyValuePath)) {
$form['#default_value'] = $formState->get($readOnlyValuePath);
}
elseif (NULL !== $definition->getDefault()) {
$form['#default_value'] = $definition->getDefault();
}
if (NULL !== $definition->getPrefix()) {
$form['#field_prefix'] = $definition->getPrefix();
}
if (NULL !== $definition->getSuffix()) {
$form['#field_suffix'] = $definition->getSuffix();
}
if (NULL !== $definition->getConst()) {
$form['#value'] = $definition->getConst();
}
if ((!$form['#disabled'] && TRUE === $formState->get('recalculateOnChange')) || $calcInitField) {
$form['#ajax'] = [
'callback' => [RecalculateCallback::class, 'onChange'],
'event' => 'change',
'progress' => [],
'disable-refocus' => TRUE,
];
}
$form += static::createBasicProperties($definition);
if ($calcInitField) {
// @phpstan-ignore offsetAccess.nonOffsetAccessible
$form['#attributes']['data-json-forms-init-calculation'] = '1';
}
return $form;
}
}