Skip to content
Snippets Groups Projects
Commit 37acc411 authored by Urvashi Vora's avatar Urvashi Vora Committed by urvashi.v
Browse files

Issue #3370924: \Drupal calls should be avoided in classes, use dependency injection instead

parent 42c3433e
No related branches found
No related tags found
1 merge request!10Issue #3370924: \Drupal calls should be avoided in classes, use dependency injection instead
......@@ -5,10 +5,14 @@ namespace Drupal\domain_language;
use Drupal\domain\DomainNegotiatorInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\domain_config\OverriderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class DomainLanguageOverrider
* Overrides configuration values based on the active domain and language.
*
* @package Drupal\domain_language
*/
class DomainLanguageOverrider implements ConfigFactoryOverrideInterface {
......@@ -16,7 +20,7 @@ class DomainLanguageOverrider implements ConfigFactoryOverrideInterface {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxy
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
......@@ -28,20 +32,29 @@ class DomainLanguageOverrider implements ConfigFactoryOverrideInterface {
protected $domainNegotiator;
/**
* A storage controller instance for reading and writing configuration data.
* The configuration factory.
*
* @var StorageInterface
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $storage;
protected $configFactory;
/**
* The domain config overrider service.
*
* @var \Drupal\domain_config\OverriderInterface
*/
protected $overrider;
/**
* The domain context of the request.
*
* @var \Drupal\domain\DomainInterface $domain
* @var \Drupal\domain\DomainInterface
*/
protected $domain;
/**
* Nested Level.
*
* @var int
*/
protected $nestedLevel;
......@@ -49,21 +62,42 @@ class DomainLanguageOverrider implements ConfigFactoryOverrideInterface {
/**
* Indicates that the request context is set.
*
* @var boolean
* @var bool
*/
protected $contextSet;
/**
* Constructs a DomainLanguageOverrider object.
*
* @param StorageInterface $storage
* The configuration storage engine.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user.
* @param \Drupal\domain\DomainNegotiatorInterface $domain_negotiator
* The domain negotiator.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
* @param \Drupal\domain_config\OverriderInterface $overrider
* The domain config overrider service.
*/
public function __construct(StorageInterface $storage) {
$this->storage = $storage;
public function __construct(AccountProxyInterface $current_user, DomainNegotiatorInterface $domain_negotiator, ConfigFactoryInterface $config_factory, OverriderInterface $overrider) {
$this->currentUser = $current_user;
$this->domainNegotiator = $domain_negotiator;
$this->configFactory = $config_factory;
$this->overrider = $overrider;
$this->nestedLevel = 0;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_user'),
$container->get('domain.negotiator'),
$container->get('config.factory'),
$container->get('domain_config.overrider')
);
}
/**
* {@inheritdoc}
*/
......@@ -79,8 +113,7 @@ class DomainLanguageOverrider implements ConfigFactoryOverrideInterface {
if ($this->domain) {
foreach ($names as $name) {
if ($name == 'system.site' && !$this->currentUser->hasPermission('bypass language restrictions')) {
$overrider = \Drupal::service('domain_config.overrider');
$configs = $overrider->loadOverrides([$name]);
$configs = $this->overrider->loadOverrides([$name]);
if (!empty($configs[$name])) {
// Initialize site settings.
......@@ -88,7 +121,7 @@ class DomainLanguageOverrider implements ConfigFactoryOverrideInterface {
}
}
elseif ($name == 'language.negotiation' && !$this->currentUser->hasPermission('bypass language restrictions')) {
$languages = \Drupal::config('domain.language.' . $this->domain->getOriginalId() . '.' . $name)->get(
$languages = $this->configFactory->getEditable('domain.language.' . $this->domain->getOriginalId() . '.' . $name)->get(
'languages'
);
......@@ -101,7 +134,7 @@ class DomainLanguageOverrider implements ConfigFactoryOverrideInterface {
// Set global config var to use second override mechanisms.
if ($this->nestedLevel == 1) {
$negotiations = \Drupal::config($name)->getRawData();
$negotiations = $this->configFactory->getEditable($name)->getRawData();
// Todo: use 'Drupal\Core\Site\Settings' when available.
// @see: https://www.drupal.org/node/2183591
......@@ -136,7 +169,7 @@ class DomainLanguageOverrider implements ConfigFactoryOverrideInterface {
/**
* {@inheritdoc}
*/
public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
public function createConfigObject($name, $collection = ConfigFactoryInterface::DEFAULT_COLLECTION) {
return NULL;
}
......@@ -163,9 +196,6 @@ class DomainLanguageOverrider implements ConfigFactoryOverrideInterface {
*/
protected function initiateContext() {
$this->contextSet = TRUE;
$this->currentUser = \Drupal::service('current_user');
$this->domainNegotiator = \Drupal::service('domain.negotiator');
// Get the domain context.
$this->domain = $this->domainNegotiator->getActiveDomain();
// If we have fired too early in the bootstrap, we must force the routine to run.
if (empty($this->domain)) {
......
......@@ -2,13 +2,17 @@
namespace Drupal\domain_language\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Routing\RouteBuilderInterface;
use Drupal\domain\DomainInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteBuilderInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class DomainLanguageForm.
......@@ -21,6 +25,80 @@ class DomainLanguageForm extends FormBase {
const DEFAULT_LANGUAGE_SITE = '***LANGUAGE_site_default***';
/**
* The route match service.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The language manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The system site configuration.
*
* @var \Drupal\Core\Config\Config
*/
protected $systemSiteConfig;
/**
* The domain site configuration.
*
* @var \Drupal\Core\Config\Config
*/
protected $domainSiteConfig;
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The router builder service.
*
* @var \Drupal\Core\Routing\RouteBuilderInterface
*/
protected $routeBuilder;
/**
* Constructs a new DomainLanguageForm object.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Core\Routing\RouteBuilderInterface $route_builder
* The router builder service.
*/
public function __construct(RouteMatchInterface $route_match, LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, RouteBuilderInterface $route_builder) {
$this->routeMatch = $route_match;
$this->languageManager = $language_manager;
$this->systemSiteConfig = $config_factory->get('system.site');
$this->configFactory = $config_factory;
$this->routeBuilder = $route_builder;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_route_match'),
$container->get('language_manager'),
$container->get('config.factory'),
$container->get('router.builder')
);
}
/**
* {@inheritdoc}
*/
......@@ -33,21 +111,21 @@ class DomainLanguageForm extends FormBase {
*/
public function buildForm(array $form, FormStateInterface $form_state) {
/** @var DomainInterface $domain */
$domain = \Drupal::routeMatch()->getParameter('domain');
$domain = $this->routeMatch->getParameter('domain');
// All available languages.
$languages = \Drupal::languageManager()->getNativeLanguages();
$languages = $this->languageManager->getNativeLanguages();
// Config without any override.
$configRaw = \Drupal::config('system.site')->getRawData();
$configRaw = $this->systemSiteConfig->getRawData();
$defaultRaw = $configRaw['default_langcode'];
// Load the domain default language.
$config = \Drupal::config('domain.config.' . $domain->getOriginalId() . '.system.site')->getRawData();
$config = $this->configFactory->get('domain.config.' . $domain->getOriginalId() . '.system.site')->getRawData();
$defaultLanguage = isset($config['default_langcode']) ? $config['default_langcode'] : self::DEFAULT_LANGUAGE_SITE;
/** @var LanguageInterface $defaultLanguageRaw */
$defaultLanguageRaw = \Drupal::languageManager()->getLanguage($defaultRaw);
$defaultLanguageRaw = $this->languageManager->getLanguage($defaultRaw);
$options = [
self::DEFAULT_LANGUAGE_SITE => $this->t(
......@@ -81,7 +159,7 @@ class DomainLanguageForm extends FormBase {
$options[$language->getId()] = $language->getName();
}
$config = \Drupal::configFactory()->get('domain.language.' . $domain->getOriginalId() . '.language.negotiation');
$config = $this->configFactory->get('domain.language.' . $domain->getOriginalId() . '.language.negotiation');
$data = $config->getRawData();
$form['languages'] = [
......@@ -125,7 +203,7 @@ class DomainLanguageForm extends FormBase {
$languages = array_filter($form_state->getValue('languages'));
// Set default language into domain config file.
$config = \Drupal::configFactory()->getEditable('domain.config.' . $domain_id . '.system.site');
$config = $this->configFactory->getEditable('domain.config.' . $domain_id . '.system.site');
if ($default_language == self::DEFAULT_LANGUAGE_SITE) {
$data = $config->getRawData();
unset($data['default_langcode']);
......@@ -145,7 +223,7 @@ class DomainLanguageForm extends FormBase {
}
// Set default language into domain language file.
$config = \Drupal::configFactory()->getEditable('domain.language.' . $domain_id . '.language.negotiation');
$config = $this->configFactory->getEditable('domain.language.' . $domain_id . '.language.negotiation');
if (empty($languages)) {
$config->delete();
}
......@@ -157,7 +235,7 @@ class DomainLanguageForm extends FormBase {
// Remove any prefixes and domains in negotiation settings from domain.config file to avoid
// any unexpected override.
$config = \Drupal::configFactory()->getEditable('domain.config.' . $domain_id . '.language.negotiation');
$config = $this->configFactory->getEditable('domain.config.' . $domain_id . '.language.negotiation');
$data = $config->getRawData();
unset($data['url']['prefixes'], $data['url']['domains']);
if (empty($data['url'])) {
......@@ -171,9 +249,7 @@ class DomainLanguageForm extends FormBase {
$config->save();
}
/** @var RouteBuilderInterface $routeBuilder */
$routeBuilder = \Drupal::service('router.builder');
$routeBuilder->rebuild();
$this->routeBuilder->rebuild();
// Redirect to domain admin page.
$form_state->setRedirect('domain.admin');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment