You are here

public function ThemeExperimentalConfirmForm::buildForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/src/Form/ThemeExperimentalConfirmForm.php \Drupal\system\Form\ThemeExperimentalConfirmForm::buildForm()
  2. 10 core/modules/system/src/Form/ThemeExperimentalConfirmForm.php \Drupal\system\Form\ThemeExperimentalConfirmForm::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 ConfirmFormBase::buildForm

File

core/modules/system/src/Form/ThemeExperimentalConfirmForm.php, line 96

Class

ThemeExperimentalConfirmForm
Builds a confirmation form for enabling experimental themes.

Namespace

Drupal\system\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $theme = $form_state
    ->getBuildInfo()['args'][0] ? $form_state
    ->getBuildInfo()['args'][0] : NULL;
  $all_themes = $this->themeList
    ->getList();
  if (!isset($all_themes[$theme])) {
    return $this
      ->redirect('system.themes_page');
  }
  $this
    ->messenger()
    ->addWarning($this
    ->t('Experimental themes are provided for testing purposes only. Use at your own risk.'));
  $dependencies = array_keys($all_themes[$theme]->requires);
  $themes = array_merge([
    $theme,
  ], $dependencies);
  $is_experimental = function ($theme) use ($all_themes) {
    return isset($all_themes[$theme]) && isset($all_themes[$theme]->info['experimental']) && $all_themes[$theme]->info['experimental'];
  };
  $get_label = function ($theme) use ($all_themes) {
    return $all_themes[$theme]->info['name'];
  };
  $items = [];
  if (!empty($dependencies)) {

    // Display a list of required themes that have to be installed as well.
    $items[] = $this
      ->formatPlural(count($dependencies), 'You must enable the @required theme to install @theme.', 'You must enable the @required themes to install @theme.', [
      '@theme' => $get_label($theme),
      // It is safe to implode this because theme names are not translated
      // markup and so will not be double-escaped.
      '@required' => implode(', ', array_map($get_label, $dependencies)),
    ]);
  }

  // Add the list of experimental themes after any other messages.
  $items[] = $this
    ->t('The following themes are experimental: @themes', [
    '@themes' => implode(', ', array_map($get_label, array_filter($themes, $is_experimental))),
  ]);
  $form['message'] = [
    '#theme' => 'item_list',
    '#items' => $items,
  ];
  return parent::buildForm($form, $form_state);
}