Skip to content

Commit

Permalink
EWPP-3393: Allow other modules to alter the langcode of the suffix ne…
Browse files Browse the repository at this point in the history
…gotiator if translation check is on.
  • Loading branch information
upchuk committed Nov 22, 2023
1 parent 5f982a0 commit dc6b22d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Drupal\oe_multilingual_url_suffix\Plugin\LanguageNegotiation;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\BubbleableMetadata;
Expand Down Expand Up @@ -59,17 +60,27 @@ class LanguageNegotiationUrlSuffix extends LanguageNegotiationUrl implements Con
*/
protected $multilingualHelper;

/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;

/**
* Constructs a new LanguageNegotiationUrlSuffix instance.
*
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
* @param \Drupal\oe_multilingual\MultilingualHelperInterface $multilingual_helper
* The multilingual helper.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler.
*/
public function __construct(EventDispatcherInterface $event_dispatcher, MultilingualHelperInterface $multilingual_helper) {
public function __construct(EventDispatcherInterface $event_dispatcher, MultilingualHelperInterface $multilingual_helper, ModuleHandlerInterface $moduleHandler) {
$this->eventDispatcher = $event_dispatcher;
$this->multilingualHelper = $multilingual_helper;
$this->moduleHandler = $moduleHandler;
}

/**
Expand All @@ -78,7 +89,8 @@ public function __construct(EventDispatcherInterface $event_dispatcher, Multilin
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$container->get('event_dispatcher'),
$container->get('oe_multilingual.helper')
$container->get('oe_multilingual.helper'),
$container->get('module_handler')
);
}

Expand Down Expand Up @@ -120,6 +132,9 @@ public function getLangcode(Request $request = NULL) {
// If we did resolve an entity, we only return the negotiated langcode
// if the entity has a translation.
if ($entity->hasTranslation($langcode)) {
// Allow other modules to control the langcode if the entity
// translation checking is enabled.
$this->moduleHandler->alter('language_negotiation_suffix_entity_translation_langcode', $langcode, $entity);
return $langcode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\Tests\oe_multilingual_url_suffix\Unit;

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\oe_multilingual_url_suffix\Plugin\LanguageNegotiation\LanguageNegotiationUrlSuffix;
Expand Down Expand Up @@ -109,7 +110,8 @@ public function testPathSuffix(string $suffix, array $suffixes, string $expected

$request = Request::create('/foo_' . $suffix, 'GET');
$helper = $this->createMock('\Drupal\oe_multilingual\MultilingualHelperInterface');
$method = new LanguageNegotiationUrlSuffix($this->eventDispatcher, $helper);
$module_handler = $this->createMock(ModuleHandlerInterface::class);
$method = new LanguageNegotiationUrlSuffix($this->eventDispatcher, $helper, $module_handler);
$method->setLanguageManager($this->languageManager);
$method->setConfig($config);
$method->setCurrentUser($this->user);
Expand Down

0 comments on commit dc6b22d

Please sign in to comment.