Skip to content

Commit

Permalink
EWPP-2963: Make use of language negotiator so that caches are properl…
Browse files Browse the repository at this point in the history
…y invalidated.
  • Loading branch information
brummbar committed Feb 17, 2023
1 parent c465d4d commit a25a66b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion oe_multilingual.services.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
oe_multilingual.language_negotiation_setter:
class: Drupal\oe_multilingual\LanguageNegotiationSetter
arguments: ['@config.factory']
arguments: ['@config.factory', '@language_negotiator']
oe_multilingual.helper:
class: Drupal\oe_multilingual\MultilingualHelper
arguments: ['@entity.repository', '@current_route_match']
Expand Down
31 changes: 23 additions & 8 deletions src/LanguageNegotiationSetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\LanguageNegotiatorInterface;

/**
* Helper service for language negotiation method configuration.
Expand All @@ -18,27 +19,39 @@ class LanguageNegotiationSetter implements LanguageNegotiationSetterInterface {
const CONFIG_NAME = 'language.types';

/**
* Drupal\Core\Config\ConfigFactoryInterface definition.
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* The language negotiator.
*
* @var \Drupal\language\LanguageNegotiatorInterface
*/
protected LanguageNegotiatorInterface $languageNegotiator;

/**
* Constructs a new LanguageNegotiationSetter object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\language\LanguageNegotiatorInterface|null $language_negotiator
* The language negotiator.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
public function __construct(ConfigFactoryInterface $config_factory, LanguageNegotiatorInterface $language_negotiator = NULL) {
$this->configFactory = $config_factory;
// Compatibility layer, will be removed in next major release.
// @todo Remove in 2.x.
$this->languageNegotiator = $language_negotiator ?? \Drupal::service('language_negotiator');
}

/**
* {@inheritdoc}
*/
public function enableNegotiationMethods(array $methods): void {
$this->configFactory
->getEditable(self::CONFIG_NAME)
->set('configurable', $methods)
->save();
$this->languageNegotiator->updateConfiguration($methods);
}

/**
Expand All @@ -47,9 +60,10 @@ public function enableNegotiationMethods(array $methods): void {
public function setInterfaceSettings(array $settings): void {
$this->configFactory
->getEditable(self::CONFIG_NAME)
->set('negotiation.' . LanguageInterface::TYPE_INTERFACE . '.enabled', $settings)
->set('negotiation.' . LanguageInterface::TYPE_INTERFACE . '.method_weights', $settings)
->save();

$this->languageNegotiator->saveConfiguration(LanguageInterface::TYPE_INTERFACE, $settings);
}

/**
Expand All @@ -72,9 +86,10 @@ public function addInterfaceSettings(array $settings): void {
public function setContentSettings(array $settings): void {
$this->configFactory
->getEditable(self::CONFIG_NAME)
->set('negotiation.' . LanguageInterface::TYPE_CONTENT . '.enabled', $settings)
->set('negotiation.' . LanguageInterface::TYPE_CONTENT . '.method_weights', $settings)
->save();

$this->languageNegotiator->saveConfiguration(LanguageInterface::TYPE_CONTENT, $settings);
}

/**
Expand Down

0 comments on commit a25a66b

Please sign in to comment.