You are here

public function StyleswitcherStyleForm::buildForm in Style Switcher 8.2

Same name and namespace in other branches
  1. 3.0.x src/Form/StyleswitcherStyleForm.php \Drupal\styleswitcher\Form\StyleswitcherStyleForm::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.

array|null $style: (optional) Style to edit. The structure of an array is the same as returned from styleswitcher_style_load().

Return value

array The form structure.

Overrides FormInterface::buildForm

See also

styleswitcher_style_load()

File

src/Form/StyleswitcherStyleForm.php, line 64

Class

StyleswitcherStyleForm
Provides a form to add/edit a style.

Namespace

Drupal\styleswitcher\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $style = NULL) {
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#description' => $this
      ->t('Human-readable name for this style.'),
    '#default_value' => $style ? $style['label'] : '',
    '#required' => TRUE,
  ];
  if ($style) {
    list(, $name_value) = explode('/', $style['name']);
  }
  $form['name'] = [
    '#type' => 'machine_name',
    '#description' => $this
      ->t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
    '#default_value' => $style ? $name_value : '',
    '#field_prefix' => 'custom/',
    '#machine_name' => [
      'source' => [
        'label',
      ],
      'exists' => [
        $this,
        'exists',
      ],
    ],
  ];
  if ($style) {

    // Show the warning about renaming styles.
    $form['name']['#description'] .= '<br />' . $this
      ->t('<strong>WARNING:</strong> if you change style machine name, users who have chosen this style will see the default one instead until they switch again.');
  }
  $form['old_name'] = [
    '#type' => 'value',
    '#value' => $style ? $style['name'] : '',
  ];
  $form['path'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Path'),
    '#description' => $this
      ->t('The path to the stylesheet file relative to the site root or an external CSS file.'),
    '#default_value' => $style ? $style['path'] : '',
    '#required' => TRUE,
    '#access' => !$style || isset($style['path']),
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#button_type' => 'primary',
  ];
  if ($style) {
    $form['actions']['delete'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Delete'),
      '#url' => Url::fromRoute('styleswitcher.style_delete', [
        'style' => $name_value,
      ]),
      '#attributes' => [
        'class' => [
          'button',
          'button--danger',
        ],
      ],
      // Do not allow to delete the blank style.
      '#access' => isset($style['path']),
    ];
  }
  return $form;
}