View source
<?php
namespace Drupal\domain_config_ui\Form;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\domain\DomainInterface;
use Drupal\domain\DomainElementManagerInterface;
use Drupal\domain_config_ui\DomainConfigUIManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SwitchForm extends FormBase {
protected $accessHandler;
public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, DomainConfigUIManager $domain_config_ui_manager, DomainElementManagerInterface $domain_element_manager) {
$this->domainConfigUiManager = $domain_config_ui_manager;
$this->languageManager = $language_manager;
$this->entityTypeManager = $entity_type_manager;
$this->domainStorage = $this->entityTypeManager
->getStorage('domain');
$this->domainElementManager = $domain_element_manager;
$this->accessHandler = $this->entityTypeManager
->getAccessControlHandler('domain');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('language_manager'), $container
->get('domain_config_ui.manager'), $container
->get('domain.element_manager'));
}
public function getFormId() {
return 'domain_config_ui_switch_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#access'] = $this
->canUseDomainConfig();
$form = $this
->addSwitchFields($form, $form_state);
return $form;
}
public function canUseDomainConfig() {
if ($this
->currentUser()
->hasPermission('administer domains')) {
$user_domains = 'all';
}
else {
$account = $this
->currentUser();
$user = $this->entityTypeManager
->getStorage('user')
->load($account
->id());
$user_domains = $this->domainElementManager
->getFieldValues($user, DomainInterface::DOMAIN_ADMIN_FIELD);
}
$permission = $this
->currentUser()
->hasPermission('use domain config ui') || $this
->currentUser()
->hasPermission('administer domain config ui');
return !empty($user_domains) && $permission;
}
public function addSwitchFields(array $form, FormStateInterface $form_state) {
$form['domain_config_ui'] = [
'#type' => 'fieldset',
'#title' => 'Domain Configuration',
'#weight' => -10,
];
if ($selected_domain_id = $this->domainConfigUiManager
->getSelectedDomainId()) {
$selected_domain = $this->domainStorage
->load($selected_domain_id);
}
$form['domain_config_ui']['domain'] = [
'#type' => 'select',
'#title' => $this
->t('Domain'),
'#options' => $this
->getDomainOptions(),
'#default_value' => !empty($selected_domain) ? $selected_domain
->id() : '',
'#ajax' => [
'callback' => '::switchCallback',
],
];
$languages = $this->languageManager
->getLanguages();
if (count($languages) > 1 && $this
->currentUser()
->hasPermission('translate domain configuration')) {
$language_options = [
'' => $this
->t('Default'),
];
foreach ($languages as $id => $language) {
if (!$language
->isLocked()) {
$language_options[$id] = $language
->getName();
}
}
$form['domain_config_ui']['language'] = [
'#type' => 'select',
'#title' => $this
->t('Language'),
'#options' => $language_options,
'#default_value' => $this->domainConfigUiManager
->getSelectedLanguageId(),
'#ajax' => [
'callback' => '::switchCallback',
],
];
$form['domain_config_ui']['help'] = [
'#markup' => $this
->t('Changing the domain or language will load its active configuration.'),
];
}
else {
$form['domain_config_ui']['help'] = [
'#markup' => $this
->t('Changing the domain will load its active configuration.'),
];
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
public static function switchCallback(array &$form, FormStateInterface $form_state) {
$request = \Drupal::service('request_stack')
->getMasterRequest();
$request_uri = $request
->getRequestUri();
if (strpos($request_uri, '//') === 0) {
$request_uri = $request
->getUri();
}
$parsed = UrlHelper::parse($request_uri);
unset($parsed['query']['ajax_form'], $parsed['query'][MainContentViewSubscriber::WRAPPER_FORMAT]);
if (\Drupal::config('domain_config_ui.settings')
->get('remember_domain')) {
$_SESSION['domain_config_ui_domain'] = $form_state
->getValue('domain');
$_SESSION['domain_config_ui_language'] = $form_state
->getValue('language');
}
else {
$parsed['query']['domain_config_ui_domain'] = $form_state
->getValue('domain');
$parsed['query']['domain_config_ui_language'] = $form_state
->getValue('language');
}
$request_uri = $parsed['path'] . ($parsed['query'] ? '?' . UrlHelper::buildQuery($parsed['query']) : '');
$response = new AjaxResponse();
$response
->addCommand(new RedirectCommand($request_uri));
return $response;
}
public function getDomainOptions() {
$domains = $this->domainStorage
->loadMultipleSorted();
$options = [];
foreach ($domains as $domain) {
$access = $this->accessHandler
->checkAccess($domain, 'view');
if ($access
->isAllowed()) {
$options[$domain
->id()] = $domain
->label();
}
}
if ($this
->currentUser()
->hasPermission('set default domain configuration')) {
$options = array_merge([
'' => $this
->t('All Domains'),
], $options);
}
return $options;
}
}