You are here

public function PanopolyThemeSelectionForm::buildForm in Panopoly 8.2

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

modules/panopoly/panopoly_theme/src/Form/PanopolyThemeSelectionForm.php, line 88

Class

PanopolyThemeSelectionForm
Provides a form to choose the starting theme.

Namespace

Drupal\panopoly_theme\Form

Code

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

  // Clear all status messages generated by modules installed in the previous
  // step.
  $this
    ->messenger()
    ->deleteByType(MessengerInterface::TYPE_STATUS);
  $form['#title'] = $this
    ->t('Choose a theme');
  $options = [];
  foreach ($this->themeHandler
    ->rebuildThemeData() as $theme) {

    // Filter hidden, test and incompatible themes.
    // This assumes test themes has 'test' in their name.
    if (!empty($theme->info['hidden']) || strpos($theme
      ->getName(), 'test') !== FALSE) {
      continue;
    }

    // Build a label with name, description and screenshot.
    $label = [
      '#type' => 'inline_template',
      '#template' => '{{ screenshot }}<span><strong>{{ name }}</strong><p>{{ description }}</p></span>',
      '#context' => [
        'name' => [
          '#markup' => $theme->info['name'],
        ],
        'description' => [
          '#markup' => $theme->info['description'],
        ],
        'screenshot' => [
          '#theme' => 'image',
          '#uri' => file_exists($theme->info['screenshot']) ? $theme->info['screenshot'] : drupal_get_path('module', 'system') . '/images/no_screenshot.png',
          '#width' => 100,
          '#alt' => $theme->info['name'],
        ],
      ],
    ];
    $options[$theme
      ->getName()] = $this->renderer
      ->renderPlain($label);
  }
  $form['theme'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Choose a theme'),
    '#title_display' => 'invisible',
    '#required' => TRUE,
    '#options' => $options,
    '#default_value' => 'bartik',
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#button_type' => 'primary',
    '#value' => $this
      ->t('Save and continue'),
  ];
  $form['#attached']['library'][] = 'panopoly_theme/selection_form';
  return $form;
}