You are here

public function StyleswitcherConfigTheme::buildForm in Style Switcher 8.2

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

string $theme: Name of the theme to configure styles for.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/StyleswitcherConfigTheme.php, line 60

Class

StyleswitcherConfigTheme
Configure theme-specific styles settings.

Namespace

Drupal\styleswitcher\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $theme = '') {
  if (!$this->themeHandler
    ->hasUi($theme)) {
    throw new NotFoundHttpException();
  }
  $styles = styleswitcher_style_load_multiple($theme);
  uasort($styles, 'styleswitcher_sort');
  $options = array_fill_keys(array_keys($styles), '');
  $form['theme_name'] = [
    '#type' => 'value',
    '#value' => $theme,
  ];
  $form['settings'] = [
    '#theme' => 'styleswitcher_admin_styles_table',
    '#tree' => TRUE,
  ];
  foreach ($styles as $name => $style) {
    $form['settings']['weight'][$name] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for @label', [
        '@label' => $style['label'],
      ]),
      '#title_display' => 'invisible',
      '#delta' => $this
        ->weightDelta($theme),
      '#default_value' => $style['weight'],
      '#weight' => $style['weight'],
      // Set special class for drag and drop updating.
      '#attributes' => [
        'class' => [
          'styleswitcher-style-weight',
        ],
      ],
    ];
    $form['settings']['name'][$name] = [
      '#theme' => 'styleswitcher_admin_style_overview',
      '#style' => $style,
    ];
    $form['settings']['label'][$name] = [
      '#markup' => $style['label'],
    ];
  }
  $form['settings']['enabled'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Enabled'),
    '#title_display' => 'invisible',
    '#options' => $options,
    '#default_value' => array_keys(styleswitcher_style_load_multiple($theme, [
      'status' => TRUE,
    ])),
  ];
  $form['settings']['default'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Default'),
    '#title_display' => 'invisible',
    '#options' => $options,
    '#default_value' => styleswitcher_default_style_key($theme),
  ];
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save configuration'),
    '#button_type' => 'primary',
  ];
  return $form;
}