You are here

PanopolyThemeSelectionForm.php in Panopoly 8.2

File

modules/panopoly/panopoly_theme/src/Form/PanopolyThemeSelectionForm.php
View source
<?php

namespace Drupal\panopoly_theme\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Extension\ThemeInstallerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form to choose the starting theme.
 */
class PanopolyThemeSelectionForm extends FormBase {

  /**
   * The theme handler service.
   *
   * @var \Drupal\Core\Extension\ThemeHandlerInterface
   */
  protected $themeHandler;

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The theme installer service.
   *
   * @var \Drupal\Core\Extension\ThemeInstallerInterface
   */
  protected $themeInstaller;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * PanopolyThemeSelectionForm constructor.
   *
   * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
   *   The theme handler service.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   * @param \Drupal\Core\Extension\ThemeInstallerInterface $theme_installer
   *   The theme installer service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(ThemeHandlerInterface $theme_handler, RendererInterface $renderer, ThemeInstallerInterface $theme_installer, ConfigFactoryInterface $config_factory) {
    $this->themeHandler = $theme_handler;
    $this->renderer = $renderer;
    $this->themeInstaller = $theme_installer;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('theme_handler'), $container
      ->get('renderer'), $container
      ->get('theme_installer'), $container
      ->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'panopoly_theme_selection_form';
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Set the default theme.
    if ($theme = $form_state
      ->getValue('theme')) {
      $this->themeInstaller
        ->install([
        $theme,
      ]);
      $this->configFactory
        ->getEditable('system.theme')
        ->set('default', $theme)
        ->save();
    }
  }

}

Classes

Namesort descending Description
PanopolyThemeSelectionForm Provides a form to choose the starting theme.