Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-8088: Fetch english external menus for alternative languages. #31

Merged
merged 8 commits into from
May 11, 2023
6 changes: 6 additions & 0 deletions helfi_navigation.module
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ function helfi_navigation_page_attachments(array &$attachments) : void {
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
->getId();

/** @var \Drupal\helfi_api_base\Language\DefaultLanguageResolver $defaultLanguageResolver */
$defaultLanguageResolver = Drupal::service('helfi_api_base.default_language_resolver');

// Languages without standard support should use fallback language in menu.
$langcode = $defaultLanguageResolver->getCurrentOrFallbackLanguage();

/** @var \Drupal\helfi_navigation\ApiManager $apiManager */
$apiManager = Drupal::service('helfi_navigation.api_manager');

Expand Down
15 changes: 13 additions & 2 deletions src/Plugin/Block/ExternalMenuBlockBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Drupal\helfi_navigation\Plugin\Block;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\helfi_navigation\ExternalMenuBlockInterface;
use Drupal\helfi_navigation\ExternalMenuTreeBuilder;
use Drupal\helfi_navigation\ApiManager;
use Drupal\helfi_navigation\ApiResponse;
use Drupal\helfi_api_base\Language\DefaultLanguageResolver;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand Down Expand Up @@ -39,6 +39,13 @@ abstract class ExternalMenuBlockBase extends MenuBlockBase implements ExternalMe
*/
protected LanguageManagerInterface $languageManager;

/**
* Default language resolver.
*
* @var \Drupal\helfi_api_base\Language\DefaultLanguageResolver
*/
protected DefaultLanguageResolver $defaultLanguageResolver;

/**
* {@inheritdoc}
*/
Expand All @@ -47,6 +54,7 @@ public static function create(ContainerInterface $container, array $configuratio
$instance->apiManager = $container->get('helfi_navigation.api_manager');
$instance->menuTreeBuilder = $container->get('helfi_navigation.external_menu_tree_builder');
$instance->languageManager = $container->get('language_manager');
$instance->defaultLanguageResolver = $container->get('helfi_api_base.default_language_resolver');
return $instance;
}

Expand Down Expand Up @@ -93,10 +101,13 @@ public function build() : array {

$menuTree = NULL;

// Languages without standard support should use fallback language in menu.
$langcode = $this->defaultLanguageResolver->getCurrentOrFallbackLanguage();

try {
$menuId = $this->getDerivativeId();
$response = $this->apiManager->get(
$this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(),
$langcode,
$menuId,
);
$menuTree = $this->menuTreeBuilder
Expand Down
15 changes: 13 additions & 2 deletions src/Plugin/Block/MobileMenuFallbackBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\helfi_navigation\ApiManager;
use Drupal\helfi_api_base\Language\DefaultLanguageResolver;
use Drupal\menu_link_content\MenuLinkContentInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -73,6 +73,13 @@ final class MobileMenuFallbackBlock extends MenuBlockBase {
*/
protected ApiManager $apiManager;

/**
* Default language resolver.
*
* @var \Drupal\helfi_api_base\Language\DefaultLanguageResolver
*/
protected DefaultLanguageResolver $defaultLanguageResolver;

/**
* {@inheritdoc}
*/
Expand All @@ -89,6 +96,7 @@ public static function create(
$instance->pathMatcher = $container->get('path.matcher');
$instance->languageManager = $container->get('language_manager');
$instance->apiManager = $container->get('helfi_navigation.api_manager');
$instance->defaultLanguageResolver = $container->get('helfi_api_base.default_language_resolver');
return $instance;
}

Expand Down Expand Up @@ -177,9 +185,12 @@ public function build() : array {
// If the current menu link is not available, we're most likely browsing
// the front page or first level of the menu tree.
// Create back and current/parent links accordingly.
// Non-primary languages should use fallback language in menu.
$langcode = $this->defaultLanguageResolver->getCurrentOrFallbackLanguage();

$url = $this->apiManager->getUrl(
'canonical',
$this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId()
$langcode,
);

$menu_link_back = [
Expand Down
37 changes: 33 additions & 4 deletions tests/src/Functional/MenuBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ class MenuBlockTest extends BrowserTestBase {
public function setUp() : void {
parent::setUp();

foreach (['fi', 'sv'] as $langcode) {
foreach (['fi', 'sv', 'es'] as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)->save();
}
$this->config('language.negotiation')
->set('url.prefixes', ['en' => 'en', 'fi' => 'fi', 'sv' => 'sv'])
->save();
->set('url.prefixes', [
'en' => 'en',
'fi' => 'fi',
'sv' => 'sv',
'es' => 'es',
])->save();

NodeType::create([
'type' => 'page',
Expand All @@ -71,6 +75,8 @@ public function testExternalMenuBlock() : void {
// 1. Mega menu has only two levels of links.
// 2. Block label is translated when a translation is provided.
// 3. Link are translated.
// Also verify exception for non-primary languages:
// 4. On languages other than fi, sv, en the external menus use en versions.
// These blocks and their content will be generated from fixtures/*.json
// files.
$expected = [
Expand Down Expand Up @@ -143,9 +149,32 @@ public function testExternalMenuBlock() : void {
],
],
],
'es' => [
'menus' => [
'External - Footer bottom navigation' => [
'Accessibility statement' => [],
],
'External - Header language links' => [
'Selkokieli' => [],
],
'External - Header top navigation' => [
'News' => [],
],
'External - Mega menu' => [
'Urban environment and traffic' => [],
'Parking' => ['lang' => 'en-GB'],
],
'City of Helsinki' => [
'Employment opportunities' => ['lang' => 'en-GB'],
],
'Connect' => [
'Feedback' => [],
],
],
],
];

foreach (['en', 'sv', 'fi'] as $language) {
foreach (['en', 'sv', 'fi', 'es'] as $language) {
$this->drupalGet('/' . $language);

['menus' => $menus] = $expected[$language];
Expand Down