SystemStateEdit.php
5.13 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* @file
* Contains \Drupal\devel\Form\SystemStateEdit.
*/
namespace Drupal\devel\Form;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form API form to edit a state.
*/
class SystemStateEdit extends FormBase {
/**
* The state store.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* Constructs a new SystemStateEdit object.
*
* @param \Drupal\Core\State\StateInterface $state
* The state service.
*/
public function __construct(StateInterface $state) {
$this->state = $state;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('state')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'devel_state_system_edit_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $state_name = '') {
// Get the old value
$old_value = $this->state->get($state_name);
if (!isset($old_value)) {
drupal_set_message(t('State !name does not exist in the system.', array('!name' => $state_name)), 'warning');
return;
}
// Only simple structures are allowed to be edited.
$disabled = !$this->checkObject($old_value);
if ($disabled) {
drupal_set_message(t('Only simple structures are allowed to be edited. State !name contains objects.', array('!name' => $state_name)), 'warning');
}
// First we will show the user the content of the variable about to be edited.
$form['value'] = array(
'#type' => 'item',
'#title' => $this->t('Current value for %name', array('%name' => $state_name)),
'#markup' => kprint_r($old_value, TRUE),
);
$transport = 'plain';
if (!$disabled && is_array($old_value)) {
try {
$old_value = Yaml::encode($old_value);
$transport = 'yaml';
}
catch (InvalidDataTypeException $e) {
drupal_set_message(t('Invalid data detected for !name : %error', array('!name' => $state_name, '%error' => $e->getMessage())), 'error');
return;
}
}
// Store in the form the name of the state variable
$form['state_name'] = array(
'#type' => 'value',
'#value' => $state_name,
);
// Set the transport format for the new value. Values:
// - plain
// - yaml
$form['transport'] = array(
'#type' => 'value',
'#value' => $transport,
);
$form['new_value'] = array(
'#type' => 'textarea',
'#title' => $this->t('New value'),
'#default_value' => $disabled ? '' : $old_value,
'#disabled' => $disabled,
'#rows' => 15,
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Save'),
'#disabled' => $disabled,
);
$form['actions']['cancel'] = array(
'#type' => 'link',
'#title' => $this->t('Cancel'),
'#url' => Url::fromRoute('devel.state_system_page')
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
if ($values['transport'] == 'yaml') {
// try to parse the new provided value
try {
$parsed_value = Yaml::decode($values['new_value']);
$form_state->setValue('parsed_value', $parsed_value);
}
catch (InvalidDataTypeException $e) {
$form_state->setErrorByName('new_value', $this->t('Invalid input: %error', array('%error' => $e->getMessage())));
}
}
else {
$form_state->setValue('parsed_value', $values['new_value']);
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Save the state
$values = $form_state->getValues();
$this->state->set($values['state_name'], $values['parsed_value']);
$form_state->setRedirectUrl(Url::fromRoute('devel.state_system_page'));
drupal_set_message($this->t('Variable %variable was successfully edited.', array('%variable' => $values['state_name'])));
$this->logger('devel')->info('Variable %variable was successfully edited.', array('%variable' => $values['state_name']));
}
/**
* Helper function to determine if a variable is or contains an object.
*
* @param $data
* Input data to check
*
* @return bool
* TRUE if the variable is not an object and does not contain one.
*/
protected function checkObject($data) {
if (is_object($data)) {
return FALSE;
}
if (is_array($data)) {
// If the current object is an array, then check recursively.
foreach ($data as $value) {
// If there is an object the whole container is "contaminated"
if (!$this->checkObject($value)) {
return FALSE;
}
}
}
// All checks pass
return TRUE;
}
}