View source
<?php
namespace Drupal\domain_theme_switch\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DomainThemeSwitchConfigForm extends ConfigFormBase {
protected $entityTypeManager;
protected $themeHandler;
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;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('theme_handler'));
}
protected function getEditableConfigNames() {
return [
'domain_theme_switch.settings',
];
}
public function getFormId() {
return 'domain_theme_switch_config_form';
}
public function getThemeList() {
$themeName = [];
foreach ($this->themeHandler
->listInfo() as $key => $value) {
$themeName[$key] = $value->info['name'];
}
return $themeName;
}
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);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
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();
}
}