You are here

public function SystemStateEdit::buildForm in Devel 8

Same name and namespace in other branches
  1. 8.3 src/Form/SystemStateEdit.php \Drupal\devel\Form\SystemStateEdit::buildForm()
  2. 8.2 src/Form/SystemStateEdit.php \Drupal\devel\Form\SystemStateEdit::buildForm()
  3. 4.x src/Form/SystemStateEdit.php \Drupal\devel\Form\SystemStateEdit::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/SystemStateEdit.php, line 54

Class

SystemStateEdit
Form API form to edit a state.

Namespace

Drupal\devel\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $state_name = '') {

  // Get the old value
  $old_value = $this->state
    ->get($state_name);
  if (!isset($old_value)) {
    drupal_set_message(t('State @name does not exist in the system.', array(
      '@name' => $state_name,
    )), 'warning');
    return;
  }

  // Only simple structures are allowed to be edited.
  $disabled = !$this
    ->checkObject($old_value);
  if ($disabled) {
    drupal_set_message(t('Only simple structures are allowed to be edited. State @name contains objects.', array(
      '@name' => $state_name,
    )), 'warning');
  }

  // First we will show the user the content of the variable about to be edited.
  $form['value'] = array(
    '#type' => 'item',
    '#title' => $this
      ->t('Current value for %name', array(
      '%name' => $state_name,
    )),
    '#markup' => kpr($old_value, TRUE),
  );
  $transport = 'plain';
  if (!$disabled && is_array($old_value)) {
    try {
      $old_value = Yaml::encode($old_value);
      $transport = 'yaml';
    } catch (InvalidDataTypeException $e) {
      drupal_set_message(t('Invalid data detected for @name : %error', array(
        '@name' => $state_name,
        '%error' => $e
          ->getMessage(),
      )), 'error');
      return;
    }
  }

  // Store in the form the name of the state variable
  $form['state_name'] = array(
    '#type' => 'value',
    '#value' => $state_name,
  );

  // Set the transport format for the new value. Values:
  //  - plain
  //  - yaml
  $form['transport'] = array(
    '#type' => 'value',
    '#value' => $transport,
  );
  $form['new_value'] = array(
    '#type' => 'textarea',
    '#title' => $this
      ->t('New value'),
    '#default_value' => $disabled ? '' : $old_value,
    '#disabled' => $disabled,
    '#rows' => 15,
  );
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#disabled' => $disabled,
  ];
  $form['actions']['cancel'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Cancel'),
    '#url' => Url::fromRoute('devel.state_system_page'),
  ];
  return $form;
}