f8c9858672b4fae95b224485792a6854e1e2a91b
[yaffs-website] / web / modules / contrib / devel / src / Form / ConfigEditor.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\Component\Utility\UrlHelper;
8 use Drupal\Core\Form\FormBase;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Url;
11
12 /**
13  * Edit config variable form.
14  */
15 class ConfigEditor extends FormBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public function getFormId() {
21     return 'devel_config_system_edit_form';
22   }
23
24   /**
25    * {@inheritdoc}
26    */
27   public function buildForm(array $form, FormStateInterface $form_state, $config_name = '') {
28     $config = $this->config($config_name);
29
30     if ($config === FALSE || $config->isNew()) {
31       drupal_set_message(t('Config @name does not exist in the system.', array('@name' => $config_name)), 'error');
32       return;
33     }
34
35     $data = $config->get();
36
37     if (empty($data)) {
38       drupal_set_message(t('Config @name exists but has no data.', array('@name' => $config_name)), 'warning');
39       return;
40     }
41
42     try {
43       $output = Yaml::encode($data);
44     }
45     catch (InvalidDataTypeException $e) {
46       drupal_set_message(t('Invalid data detected for @name : %error', array('@name' => $config_name, '%error' => $e->getMessage())), 'error');
47       return;
48     }
49
50     $form['current'] = array(
51       '#type' => 'details',
52       '#title' => $this->t('Current value for %variable', array('%variable' => $config_name)),
53       '#attributes' => array('class' => array('container-inline')),
54     );
55     $form['current']['value'] = array(
56       '#type' => 'item',
57       '#markup' => dpr($output, TRUE),
58     );
59
60     $form['name'] = array(
61       '#type' => 'value',
62       '#value' => $config_name,
63     );
64     $form['new'] = array(
65       '#type' => 'textarea',
66       '#title' => $this->t('New value'),
67       '#default_value' => $output,
68       '#rows' => 24,
69       '#required' => TRUE,
70     );
71
72     $form['actions'] = array('#type' => 'actions');
73     $form['actions']['submit'] = array(
74       '#type' => 'submit',
75       '#value' => $this->t('Save'),
76     );
77     $form['actions']['cancel'] = array(
78       '#type' => 'link',
79       '#title' => $this->t('Cancel'),
80       '#url' => $this->buildCancelLinkUrl(),
81     );
82
83     return $form;
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public function validateForm(array &$form, FormStateInterface $form_state) {
90     $value = $form_state->getValue('new');
91     // try to parse the new provided value
92     try {
93       $parsed_value = Yaml::decode($value);
94       // Config::setData needs array for the new configuration and
95       // a simple string is valid YAML for any reason.
96       if (is_array($parsed_value)) {
97         $form_state->setValue('parsed_value', $parsed_value);
98       }
99       else {
100         $form_state->setErrorByName('new', $this->t('Invalid input'));
101       }
102     }
103     catch (InvalidDataTypeException $e) {
104       $form_state->setErrorByName('new', $this->t('Invalid input: %error', array('%error' => $e->getMessage())));
105     }
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function submitForm(array &$form, FormStateInterface $form_state) {
112     $values = $form_state->getValues();
113     try {
114       $this->configFactory()->getEditable($values['name'])->setData($values['parsed_value'])->save();
115       drupal_set_message($this->t('Configuration variable %variable was successfully saved.', array('%variable' => $values['name'])));
116       $this->logger('devel')->info('Configuration variable %variable was successfully saved.', array('%variable' => $values['name']));
117
118       $form_state->setRedirectUrl(Url::fromRoute('devel.configs_list'));
119     }
120     catch (\Exception $e) {
121       drupal_set_message($e->getMessage(), 'error');
122       $this->logger('devel')->error('Error saving configuration variable %variable : %error.', array('%variable' => $values['name'], '%error' => $e->getMessage()));
123     }
124   }
125
126   /**
127    * Builds the cancel link url for the form.
128    *
129    * @return Url
130    *   Cancel url
131    */
132   private function buildCancelLinkUrl() {
133     $query = $this->getRequest()->query;
134
135     if ($query->has('destination')) {
136       $options = UrlHelper::parse($query->get('destination'));
137       $url = Url::fromUri('internal:/' . $options['path'], $options);
138     }
139     else {
140       $url = Url::fromRoute('devel.configs_list');
141     }
142
143     return $url;
144   }
145
146 }