d4ba0eda48d1a3683bee7385acaef92ad14a205c
[yaffs-website] / web / modules / contrib / devel / src / Form / SystemStateEdit.php
1 <?php
2
3 namespace Drupal\devel\Form;
4
5 use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
6 use Drupal\Component\Serialization\Yaml;
7 use Drupal\Core\Form\FormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\State\StateInterface;
10 use Drupal\Core\Url;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Form API form to edit a state.
15  */
16 class SystemStateEdit extends FormBase {
17
18   /**
19    * The state store.
20    *
21    * @var \Drupal\Core\State\StateInterface
22    */
23   protected $state;
24
25   /**
26    * Constructs a new SystemStateEdit object.
27    *
28    * @param \Drupal\Core\State\StateInterface $state
29    *   The state service.
30    */
31   public function __construct(StateInterface $state) {
32     $this->state = $state;
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public static function create(ContainerInterface $container) {
39     return new static(
40       $container->get('state')
41     );
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function getFormId() {
48     return 'devel_state_system_edit_form';
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function buildForm(array $form, FormStateInterface $form_state, $state_name = '') {
55     // Get the old value
56     $old_value = $this->state->get($state_name);
57
58     if (!isset($old_value)) {
59       drupal_set_message(t('State @name does not exist in the system.', array('@name' => $state_name)), 'warning');
60       return;
61     }
62
63     // Only simple structures are allowed to be edited.
64     $disabled = !$this->checkObject($old_value);
65
66     if ($disabled) {
67       drupal_set_message(t('Only simple structures are allowed to be edited. State @name contains objects.', array('@name' => $state_name)), 'warning');
68     }
69
70     // First we will show the user the content of the variable about to be edited.
71     $form['value'] = array(
72       '#type' => 'item',
73       '#title' => $this->t('Current value for %name', array('%name' => $state_name)),
74       '#markup' => kpr($old_value, TRUE),
75     );
76
77     $transport = 'plain';
78
79     if (!$disabled && is_array($old_value)) {
80       try {
81         $old_value = Yaml::encode($old_value);
82         $transport = 'yaml';
83       }
84       catch (InvalidDataTypeException $e) {
85         drupal_set_message(t('Invalid data detected for @name : %error', array('@name' => $state_name, '%error' => $e->getMessage())), 'error');
86         return;
87       }
88     }
89
90     // Store in the form the name of the state variable
91     $form['state_name'] = array(
92       '#type' => 'value',
93       '#value' => $state_name,
94     );
95     // Set the transport format for the new value. Values:
96     //  - plain
97     //  - yaml
98     $form['transport'] = array(
99       '#type' => 'value',
100       '#value' => $transport,
101     );
102
103     $form['new_value'] = array(
104       '#type' => 'textarea',
105       '#title' => $this->t('New value'),
106       '#default_value' => $disabled ? '' : $old_value,
107       '#disabled' => $disabled,
108       '#rows' => 15,
109     );
110
111     $form['actions'] = array('#type' => 'actions');
112     $form['actions']['submit'] = array(
113       '#type' => 'submit',
114       '#value' => $this->t('Save'),
115       '#disabled' => $disabled,
116     );
117     $form['actions']['cancel'] = array(
118       '#type' => 'link',
119       '#title' => $this->t('Cancel'),
120       '#url' => Url::fromRoute('devel.state_system_page')
121     );
122
123     return $form;
124   }
125
126   /**
127    * {@inheritdoc}
128    */
129   public function validateForm(array &$form, FormStateInterface $form_state) {
130     $values = $form_state->getValues();
131
132     if ($values['transport'] == 'yaml') {
133       // try to parse the new provided value
134       try {
135         $parsed_value = Yaml::decode($values['new_value']);
136         $form_state->setValue('parsed_value', $parsed_value);
137       }
138       catch (InvalidDataTypeException $e) {
139         $form_state->setErrorByName('new_value', $this->t('Invalid input: %error', array('%error' => $e->getMessage())));
140       }
141     }
142     else {
143       $form_state->setValue('parsed_value', $values['new_value']);
144     }
145
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   public function submitForm(array &$form, FormStateInterface $form_state) {
152     // Save the state
153     $values = $form_state->getValues();
154     $this->state->set($values['state_name'], $values['parsed_value']);
155
156     $form_state->setRedirectUrl(Url::fromRoute('devel.state_system_page'));
157
158     drupal_set_message($this->t('Variable %variable was successfully edited.', array('%variable' => $values['state_name'])));
159     $this->logger('devel')->info('Variable %variable was successfully edited.', array('%variable' => $values['state_name']));
160   }
161
162   /**
163    * Helper function to determine if a variable is or contains an object.
164    *
165    * @param $data
166    *   Input data to check
167    *
168    * @return bool
169    *   TRUE if the variable is not an object and does not contain one.
170    */
171   protected function checkObject($data) {
172     if (is_object($data)) {
173       return FALSE;
174     }
175     if (is_array($data)) {
176       // If the current object is an array, then check recursively.
177       foreach ($data as $value) {
178         // If there is an object the whole container is "contaminated"
179         if (!$this->checkObject($value)) {
180           return FALSE;
181         }
182       }
183     }
184
185     // All checks pass
186     return TRUE;
187   }
188
189 }