You are here

class PanopolyThemeSelectionForm in Panopoly 8.2

Provides a form to choose the starting theme.

Hierarchy

Expanded class hierarchy of PanopolyThemeSelectionForm

1 file declares its use of PanopolyThemeSelectionForm
panopoly_theme.profile.inc in modules/panopoly/panopoly_theme/panopoly_theme.profile.inc
Provides an install task for selection a default theme.

File

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

Namespace

Drupal\panopoly_theme\Form
View source
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();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PanopolyThemeSelectionForm::$configFactory protected property The config factory. Overrides FormBase::$configFactory
PanopolyThemeSelectionForm::$renderer protected property The renderer service.
PanopolyThemeSelectionForm::$themeHandler protected property The theme handler service.
PanopolyThemeSelectionForm::$themeInstaller protected property The theme installer service.
PanopolyThemeSelectionForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
PanopolyThemeSelectionForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
PanopolyThemeSelectionForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
PanopolyThemeSelectionForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
PanopolyThemeSelectionForm::__construct public function PanopolyThemeSelectionForm constructor.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.