3 namespace Drupal\devel\Form;
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;
13 * Edit config variable form.
15 class ConfigEditor extends FormBase {
20 public function getFormId() {
21 return 'devel_config_system_edit_form';
27 public function buildForm(array $form, FormStateInterface $form_state, $config_name = '') {
28 $config = $this->config($config_name);
30 if ($config === FALSE || $config->isNew()) {
31 drupal_set_message(t('Config @name does not exist in the system.', array('@name' => $config_name)), 'error');
35 $data = $config->get();
38 drupal_set_message(t('Config @name exists but has no data.', array('@name' => $config_name)), 'warning');
43 $output = Yaml::encode($data);
45 catch (InvalidDataTypeException $e) {
46 drupal_set_message(t('Invalid data detected for @name : %error', array('@name' => $config_name, '%error' => $e->getMessage())), 'error');
50 $form['current'] = array(
52 '#title' => $this->t('Current value for %variable', array('%variable' => $config_name)),
53 '#attributes' => array('class' => array('container-inline')),
55 $form['current']['value'] = array(
57 '#markup' => dpr($output, TRUE),
60 $form['name'] = array(
62 '#value' => $config_name,
65 '#type' => 'textarea',
66 '#title' => $this->t('New value'),
67 '#default_value' => $output,
72 $form['actions'] = array('#type' => 'actions');
73 $form['actions']['submit'] = array(
75 '#value' => $this->t('Save'),
77 $form['actions']['cancel'] = array(
79 '#title' => $this->t('Cancel'),
80 '#url' => $this->buildCancelLinkUrl(),
89 public function validateForm(array &$form, FormStateInterface $form_state) {
90 $value = $form_state->getValue('new');
91 // try to parse the new provided value
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);
100 $form_state->setErrorByName('new', $this->t('Invalid input'));
103 catch (InvalidDataTypeException $e) {
104 $form_state->setErrorByName('new', $this->t('Invalid input: %error', array('%error' => $e->getMessage())));
111 public function submitForm(array &$form, FormStateInterface $form_state) {
112 $values = $form_state->getValues();
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']));
118 $form_state->setRedirectUrl(Url::fromRoute('devel.configs_list'));
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()));
127 * Builds the cancel link url for the form.
132 private function buildCancelLinkUrl() {
133 $query = $this->getRequest()->query;
135 if ($query->has('destination')) {
136 $options = UrlHelper::parse($query->get('destination'));
137 $url = Url::fromUri('internal:/' . $options['path'], $options);
140 $url = Url::fromRoute('devel.configs_list');