function ConfigEditor::buildForm

Same name in other branches
  1. 8.x-1.x src/Form/ConfigEditor.php \Drupal\devel\Form\ConfigEditor::buildForm()
  2. 5.x src/Form/ConfigEditor.php \Drupal\devel\Form\ConfigEditor::buildForm()

Overrides FormInterface::buildForm

File

src/Form/ConfigEditor.php, line 27

Class

ConfigEditor
Edit config variable form.

Namespace

Drupal\devel\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $config_name = '') {
    $config = $this->config($config_name);
    if ($config === FALSE || $config->isNew()) {
        $this->messenger()
            ->addError($this->t('Config @name does not exist in the system.', [
            '@name' => $config_name,
        ]));
        return;
    }
    $data = $config->getOriginal();
    if (empty($data)) {
        $this->messenger()
            ->addWarning($this->t('Config @name exists but has no data.', [
            '@name' => $config_name,
        ]));
        return;
    }
    try {
        $output = Yaml::encode($data);
    } catch (InvalidDataTypeException $e) {
        $this->messenger()
            ->addError($this->t('Invalid data detected for @name : %error', [
            '@name' => $config_name,
            '%error' => $e->getMessage(),
        ]));
        return;
    }
    $form['current'] = [
        '#type' => 'details',
        '#title' => $this->t('Current value for %variable', [
            '%variable' => $config_name,
        ]),
        '#attributes' => [
            'class' => [
                'container-inline',
            ],
        ],
    ];
    $form['current']['value'] = [
        '#type' => 'item',
        // phpcs:ignore Drupal.Functions.DiscouragedFunctions
'#markup' => dpr($output, TRUE),
    ];
    $form['name'] = [
        '#type' => 'value',
        '#value' => $config_name,
    ];
    $form['new'] = [
        '#type' => 'textarea',
        '#title' => $this->t('New value'),
        '#default_value' => $output,
        '#rows' => 24,
        '#required' => TRUE,
    ];
    $form['actions'] = [
        '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Save'),
    ];
    $form['actions']['cancel'] = [
        '#type' => 'link',
        '#title' => $this->t('Cancel'),
        '#url' => $this->buildCancelLinkUrl(),
    ];
    $form['actions']['delete'] = [
        '#type' => 'link',
        '#title' => $this->t('Delete'),
        '#url' => Url::fromRoute('devel.config_delete', [
            'config_name' => $config_name,
        ]),
        '#attributes' => [
            'class' => [
                'button',
                'button--danger',
            ],
        ],
    ];
    return $form;
}