function ConfigEditor::buildForm

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

Overrides FormInterface::buildForm

File

src/Form/ConfigEditor.php, line 55

Class

ConfigEditor
Edit config variable form.

Namespace

Drupal\devel\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $config_name = '') : array {
    $config = $this->configFactory
        ->get($config_name);
    if ($config->isNew()) {
        $this->messenger
            ->addError($this->t('Config @name does not exist in the system.', [
            '@name' => $config_name,
        ]));
        return $form;
    }
    $data = $config->getOriginal();
    if (empty($data)) {
        $this->messenger
            ->addWarning($this->t('Config @name exists but has no data.', [
            '@name' => $config_name,
        ]));
        return $form;
    }
    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;
    }
    $form['current'] = [
        '#type' => 'details',
        '#title' => $this->t('Current value for %variable', [
            '%variable' => $config_name,
        ]),
        '#attributes' => [
            'class' => [
                'container-inline',
            ],
        ],
    ];
    $form['current']['value'] = [
        '#type' => 'item',
        '#markup' => $this->dumper
            ->dumpOrExport(input: $output, plugin_id: 'default'),
    ];
    $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;
}