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

OPENEUROPA-1754: Move content language switcher to header. #55

Merged
merged 12 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ default:
region_map:
"language switcher": "#block-oe-multilingual-language-switcher"
"language dialog": "#block-oe-multilingual-language-switcher"
page header: ".region-content"
"page content": ".region-content"
formatters:
progress: ~
3 changes: 3 additions & 0 deletions oe_multilingual.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ services:
oe_multilingual.helper:
class: Drupal\oe_multilingual\MultilingualHelper
arguments: ['@entity.repository', '@current_route_match']
oe_multilingual.language_provider:
imanoleguskiza marked this conversation as resolved.
Show resolved Hide resolved
class: Drupal\oe_multilingual\LanguageProvider
arguments: ['@language_manager', '@path.matcher', '@oe_multilingual.helper']
oe_multilingual.content_language_settings:
class: Drupal\oe_multilingual\MultilingualConfigOverride
tags:
Expand Down
80 changes: 80 additions & 0 deletions src/LanguageProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_multilingual;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Url;

/**
* Helper service around multilingual functionalities.
*/
class LanguageProvider {

/**
* The multilingual helper service.
*
* @var \Drupal\oe_multilingual\MultilingualHelperInterface
*/
protected $multilingualHelper;

/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;

/**
* The path matcher.
*
* @var \Drupal\Core\Path\PathMatcherInterface
*/
protected $pathMatcher;

/**
* Constructs an ContentLanguageBlock object.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
* The path matcher.
* @param \Drupal\oe_multilingual\MultilingualHelperInterface $multilingual_helper
* The multilingual helper service.
*/
public function __construct(LanguageManagerInterface $language_manager, PathMatcherInterface $path_matcher, MultilingualHelperInterface $multilingual_helper) {
$this->languageManager = $language_manager;
$this->pathMatcher = $path_matcher;
$this->multilingualHelper = $multilingual_helper;
}

/**
* Returns a list of available translation links for a given entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The language manager.
*
* @return array
* Array of available translation links.
*/
public function getEntityAvailableLanguages(EntityInterface $entity) {
$route_name = $this->pathMatcher->isFrontPage() ? '<front>' : '<current>';
$links = $this->languageManager->getLanguageSwitchLinks(LanguageInterface::TYPE_CONTENT, Url::fromRoute($route_name));

$available_languages = [];
if (isset($links->links)) {
// Only show links to the available translation languages except the
// current one.
$available_languages = array_intersect_key($links->links, $entity->getTranslationLanguages());
$translation = $this->multilingualHelper->getCurrentLanguageEntityTranslation($entity);
unset($available_languages[$translation->language()->getId()]);
}

return $available_languages;
}

}
46 changes: 24 additions & 22 deletions src/Plugin/Block/ContentLanguageBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\language\Plugin\Block\LanguageBlock;
use Drupal\oe_multilingual\MultilingualHelperInterface;
use Drupal\oe_multilingual\LanguageProvider;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand All @@ -32,6 +31,13 @@ class ContentLanguageBlock extends LanguageBlock implements ContainerFactoryPlug
*/
protected $multilingualHelper;

/**
* The language provider.
imanoleguskiza marked this conversation as resolved.
Show resolved Hide resolved
*
* @var \Drupal\oe_multilingual\LanguageProvider
*/
protected $languageProvider;

/**
* Constructs an ContentLanguageBlock object.
*
Expand All @@ -47,10 +53,13 @@ class ContentLanguageBlock extends LanguageBlock implements ContainerFactoryPlug
* The path matcher.
* @param \Drupal\oe_multilingual\MultilingualHelperInterface $multilingual_helper
* The multilingual helper service.
* @param \Drupal\oe_multilingual\LanguageProvider $language_provider
* The language provider.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, PathMatcherInterface $path_matcher, MultilingualHelperInterface $multilingual_helper) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, PathMatcherInterface $path_matcher, MultilingualHelperInterface $multilingual_helper, LanguageProvider $language_provider) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $language_manager, $path_matcher);
$this->multilingualHelper = $multilingual_helper;
$this->languageProvider = $language_provider;
}

/**
Expand All @@ -63,7 +72,8 @@ public static function create(ContainerInterface $container, array $configuratio
$plugin_definition,
$container->get('language_manager'),
$container->get('path.matcher'),
$container->get('oe_multilingual.helper')
$container->get('oe_multilingual.helper'),
$container->get('oe_multilingual.language_provider')
);
}

Expand All @@ -86,26 +96,18 @@ public function build() {
return $build;
}

$route_name = $this->pathMatcher->isFrontPage() ? '<front>' : '<current>';
$links = $this->languageManager->getLanguageSwitchLinks(LanguageInterface::TYPE_CONTENT, Url::fromRoute($route_name));

if (isset($links->links)) {
// Only show links to the available translation languages except the
// current one.
$available_languages = array_intersect_key($links->links, $entity->getTranslationLanguages());
unset($available_languages[$translation->language()->getId()]);
$available_languages = $this->languageProvider->getEntityAvailableLanguages($entity);

$build = [
'#theme' => 'links__oe_multilingual_content_language_block',
'#links' => $available_languages,
'#attributes' => [
'class' => [
"language-switcher-{$links->method_id}",
],
$build = [
upchuk marked this conversation as resolved.
Show resolved Hide resolved
'#theme' => 'links__oe_multilingual_content_language_block',
'#links' => $available_languages,
'#attributes' => [
'class' => [
"language-switcher",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this class here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why it was there in the first place ... :(

],
'#set_active_class' => TRUE,
];
}
],
'#set_active_class' => TRUE,
];
upchuk marked this conversation as resolved.
Show resolved Hide resolved

return $build;
}
Expand Down
34 changes: 24 additions & 10 deletions tests/features/content-language-switcher.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,41 @@ Feature: Content language selector
Given the following "Demo translatable page" content item:
| Title | Page title |
| Body | Page body |
And the following "Italian" translation for the "Demo translatable page" with title "Page title":
| Title | Titolo pagina |
| Body | Testo pagina |
And the following "Greek" translation for the "Demo translatable page" with title "Page title":
| Title | Τίτλος σελίδας |
| Body | Σελίδα κειμένου |
And the following "Spanish" translation for the "Demo translatable page" with title "Page title":
| Title | Título de página |
| Body | Texto de página |
And the following "Italian" translation for the "Demo translatable page" with title "Page title":
| Title | Titolo pagina |
| Body | Testo pagina |

Scenario: Visitor navigating to an available translation shouldn't see the language selector
Given I visit the "Page title" content
When I visit the "Page title" content
Then I should see the heading "Page title"
And I should see "Page body"
And I should not see the link "Spanish" in the "page header" region

And I should not see the link "ελληνικά" in the "page content" region
And I should not see the link "español" in the "page content" region
And I should not see the link "italiano" in the "page content" region

Scenario: Visitor navigating to an available translation shouldn't see the language selector
When I visit the "Page title" content
And I click "italiano"
Then I should see the heading "Titolo pagina"
And I should see "Testo pagina"

And I should not see the link "ελληνικά" in the "page content" region
And I should not see the link "English" in the "page content" region
And I should not see the link "español" in the "page content" region
imanoleguskiza marked this conversation as resolved.
Show resolved Hide resolved

Scenario: Visitor navigating to an unavailable translation should see the language selector
Given I visit the "Page title" content
When I visit the "Page title" content
And I click "Deutsch"
Then I should see the heading "Page title"
And I should see "Page body"
When I click "български"
Then I should see the heading "Page title"
And I should see "Page body"
And I should see the link "español" in the "page header" region

And I should see the link "ελληνικά" in the "page content" region
And I should see the link "español" in the "page content" region
And I should see the link "italiano" in the "page content" region
imanoleguskiza marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 4 additions & 4 deletions tests/features/translation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Feature: Translate content
And I press "Save"

Then I should see the success message "Demo translatable page Test page has been created."
And I should see "Test page" in the "page header"
And I should see "This is a test" in the "page header"
And I should see "Test page" in the "page content"
And I should see "This is a test" in the "page content"

# Translate the Translatable page content into Spanish.
When I click "Translate"
Expand All @@ -25,6 +25,6 @@ Feature: Translate content
And I press "Save (this translation)"

Then I should see the success message "Demo translatable page Página de prueba has been updated."
And I should see "Página de prueba" in the "page header"
And I should see "Esto es una prueba" in the "page header"
And I should see "Página de prueba" in the "page content"
And I should see "Esto es una prueba" in the "page content"