You are here

class DomainThemeSwitchConfigForm in Domain Theme Switch 8

Class DomainThemeSwitchConfigForm.

@package Drupal\domain_theme_switch\Form

Hierarchy

Expanded class hierarchy of DomainThemeSwitchConfigForm

1 string reference to 'DomainThemeSwitchConfigForm'
domain_theme_switch.routing.yml in ./domain_theme_switch.routing.yml
domain_theme_switch.routing.yml

File

src/Form/DomainThemeSwitchConfigForm.php, line 19

Namespace

Drupal\domain_theme_switch\Form
View source
class DomainThemeSwitchConfigForm extends ConfigFormBase {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

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

  /**
   * Construct function.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory load.
   * @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
   *   The theme handler.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManager $entity_type_manager, ThemeHandlerInterface $theme_handler) {
    parent::__construct($config_factory);
    $this->entityTypeManager = $entity_type_manager;
    $this->themeHandler = $theme_handler;
  }

  /**
   * Create function return static domain loader configuration.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   Load the ContainerInterface.
   *
   * @return static
   *   return domain loader configuration.
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('entity_type.manager'), $container
      ->get('theme_handler'));
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'domain_theme_switch.settings',
    ];
  }

  /**
   * Form ID is domain_theme_switch_config_form.
   *
   * @return string
   *   Return form ID.
   */
  public function getFormId() {
    return 'domain_theme_switch_config_form';
  }

  /**
   * Function to get the list of installed themes.
   *
   * @return array
   *   The complete theme registry data array.
   */
  public function getThemeList() {
    $themeName = [];
    foreach ($this->themeHandler
      ->listInfo() as $key => $value) {
      $themeName[$key] = $value->info['name'];
    }
    return $themeName;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('domain_theme_switch.settings');
    $defaultSiteTheme = $this
      ->config('system.theme')
      ->get('default');
    $defaultAdminTheme = $this
      ->config('system.theme')
      ->get('admin');
    $themeNames = $this
      ->getThemeList();
    $domains = $this->entityTypeManager
      ->getStorage('domain')
      ->loadMultiple();
    foreach ($domains as $domain) {
      $domainId = $domain
        ->id();
      $hostname = $domain
        ->get('name');
      $form[$domainId] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Select Theme for "@domain"', [
          '@domain' => $hostname,
        ]),
      ];
      $form[$domainId][$domainId . '_site'] = [
        '#title' => $this
          ->t('Site theme for domain'),
        '#type' => 'select',
        '#options' => $themeNames,
        '#default_value' => NULL !== $config
          ->get($domainId . '_site') ? $config
          ->get($domainId . '_site') : $defaultSiteTheme,
      ];
      $form[$domainId][$domainId . '_admin'] = [
        '#title' => $this
          ->t('Admin theme for domain'),
        '#suffix' => $this
          ->t('Change permission to allow domain admin theme @link.', [
          '@link' => Link::fromTextAndUrl($this
            ->t('change permission'), Url::fromRoute('user.admin_permissions', [], [
            'fragment' => 'module-domain_theme_switch',
          ]))
            ->toString(),
        ]),
        '#type' => 'select',
        '#options' => $themeNames,
        '#default_value' => NULL !== $config
          ->get($domainId . '_admin') ? $config
          ->get($domainId . '_admin') : $defaultAdminTheme,
      ];
    }
    if (count($domains) === 0) {
      $form['domain_theme_switch_message'] = [
        '#markup' => $this
          ->t('Zero domain records found. Please @link to create the domain.', [
          '@link' => Link::fromTextAndUrl($this
            ->t('click here'), Url::fromRoute('domain.admin'))
            ->toString(),
        ]),
      ];
      return $form;
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * Validate function for the form.
   *
   * @param array $form
   *   Form items.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Formstate for validate.
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $domains = $this->entityTypeManager
      ->getStorage('domain')
      ->loadMultiple();
    $config = $this
      ->config('domain_theme_switch.settings');
    foreach ($domains as $domain) {
      $domainId = $domain
        ->id();
      $config
        ->set($domainId . '_site', $form_state
        ->getValue($domainId . '_site'));
      $config
        ->set($domainId . '_admin', $form_state
        ->getValue($domainId . '_admin'));
    }
    $config
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
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
DomainThemeSwitchConfigForm::$entityTypeManager protected property The entity type manager.
DomainThemeSwitchConfigForm::$themeHandler protected property The theme handler.
DomainThemeSwitchConfigForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
DomainThemeSwitchConfigForm::create public static function Create function return static domain loader configuration. Overrides ConfigFormBase::create
DomainThemeSwitchConfigForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
DomainThemeSwitchConfigForm::getFormId public function Form ID is domain_theme_switch_config_form. Overrides FormInterface::getFormId
DomainThemeSwitchConfigForm::getThemeList public function Function to get the list of installed themes.
DomainThemeSwitchConfigForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
DomainThemeSwitchConfigForm::validateForm public function Validate function for the form. Overrides FormBase::validateForm
DomainThemeSwitchConfigForm::__construct public function Construct function. Overrides ConfigFormBase::__construct
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
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.
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.
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.