You are here

public function ConfigEditor::buildForm in Devel 8.3

Same name and namespace in other branches
  1. 8 src/Form/ConfigEditor.php \Drupal\devel\Form\ConfigEditor::buildForm()
  2. 8.2 src/Form/ConfigEditor.php \Drupal\devel\Form\ConfigEditor::buildForm()
  3. 4.x src/Form/ConfigEditor.php \Drupal\devel\Form\ConfigEditor::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

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',
    '#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;
}