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

OEL-1167: Theme the content language switcher #94

Merged
merged 36 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8c6ef00
OEL-1167: Create oe_whitelabel_multilingual submodule.
Mar 10, 2022
54fb4b1
OEL-1167: Create the language switcher block template.
Mar 10, 2022
4633bca
OEL-1167: Preprocess variables for the language switcher template.
Mar 10, 2022
3ed54e2
OEL-1167: Refactor oe_whitelabel_multilingual.module.
Mar 14, 2022
ec1ab50
OEL-1167: Use placeholders on alert text and add a margin bottom to s…
Mar 14, 2022
d2d4536
OEL-1167: Create whitelabel multilingual kernel test.
Mar 14, 2022
a369c61
OEL-1167: Move template to avoid dependency on oe_whitelabel_multilin…
GilNovacomm Mar 16, 2022
af44289
OEL-1167: Update tests.
GilNovacomm Mar 16, 2022
ab65900
OEL-1167: Fix library commentary, and link icon definition.
GilNovacomm Mar 16, 2022
a58da02
OEL-1167: Remove unecessary registry empty statement.
GilNovacomm Mar 16, 2022
e812d79
OEL-1167: Assert block is not shown in test when viewing node on its …
GilNovacomm Mar 16, 2022
8e06e95
OEL-1167: Enable oe_whitelabel_multilingual during installation.
GilNovacomm Mar 16, 2022
7f61d64
OEL-1167: Sort test module dependencies and remove duplicated block r…
GilNovacomm Mar 16, 2022
52f27eb
OEL-1167: Code standards.
GilNovacomm Mar 16, 2022
862198b
OEL-1167: Update template comment and icon path.
GilNovacomm Mar 16, 2022
6bdc691
OEL-1167: Place the main page content block to ensure block order pla…
GilNovacomm Mar 16, 2022
b90af4f
OEL-1167: Update config dependency.
GilNovacomm Mar 16, 2022
d6111df
OEL-1167: Place the language switcher block on the page content region.
GilNovacomm Mar 17, 2022
d0341e0
OEL-1167: Move blocks to root module config/optional folder.
GilNovacomm Mar 17, 2022
44095f0
OEL-1167: Remove config dependency.
GilNovacomm Mar 17, 2022
c956a83
OEL-1167: Update dependencies and remove unexisting cas.settings.yml.
GilNovacomm Mar 17, 2022
bdeaf6a
OEL-1167: Configuration moved from oe_whitelabel_multiglingual.
GilNovacomm Mar 17, 2022
7eef2e1
OEL-1167: Rename multiligual lang swticher block and move to theme ro…
GilNovacomm Mar 17, 2022
7e6ce8a
OEL-1167: Rename content language switcher block.
drishu Mar 18, 2022
d7fc007
OEL-1167: Remove redundant variables.
drishu Mar 18, 2022
94185c9
OEL-1167: Fixup test.
drishu Mar 18, 2022
1b27d53
OEL-1167: Export block config.
drishu Mar 18, 2022
7bf25c4
OEL-1167: Rename content switcher in config_devel.
drishu Mar 18, 2022
1586043
OEL-1167: Remove content switcher block export.
drishu Mar 18, 2022
777b1ae
OEL-1167: Remove language_block preprocess from root.
drishu Mar 18, 2022
de3b993
OEL-1167: Fix typo in links block preprocess.
drishu Mar 18, 2022
5eaf632
OEL-1167: Add new module dependency to multilingual kernel test.
drishu Mar 18, 2022
bc274a4
OEL-1167: Account for language und.
drishu Mar 21, 2022
e5b2f37
OEL-1167: Generate expandable id.
drishu Mar 21, 2022
34998d5
OEL-1167: Fix config_devel settings.
drishu Mar 21, 2022
023a770
OEL-1167: Move var doc to multilingual helper service.
drishu Mar 21, 2022
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 config/optional/block.block.oe_whitelabel_branding.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ plugin: system_branding_block
settings:
id: system_branding_block
label: 'Site branding'
provider: system
label_display: '0'
provider: system
use_site_logo: true
use_site_name: true
use_site_slogan: true
Expand Down
2 changes: 1 addition & 1 deletion config/optional/block.block.oe_whitelabel_breadcrumbs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ plugin: system_breadcrumb_block
settings:
id: system_breadcrumb_block
label: Breadcrumbs
provider: system
label_display: '0'
provider: system
visibility: { }
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ status: true
dependencies:
module:
- oe_corporate_blocks
- oe_whitelabel_helper
donquixote marked this conversation as resolved.
Show resolved Hide resolved
theme:
- oe_whitelabel
id: oe_whitelabel_ec_corporate_footer
Expand All @@ -14,11 +15,11 @@ plugin: oe_corporate_blocks_ec_footer
settings:
id: oe_corporate_blocks_ec_footer
label: 'EC Footer block'
provider: oe_corporate_blocks
label_display: '0'
provider: oe_corporate_blocks
visibility:
oe_whitelabel_helper_current_component_library:
id: oe_whitelabel_helper_current_component_library
component_library: ec
negate: false
context_mapping: { }
component_library: ec
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ status: true
dependencies:
module:
- oe_corporate_blocks
- oe_whitelabel_helper
theme:
- oe_whitelabel
id: oe_whitelabel_eu_corporate_footer
Expand All @@ -14,11 +15,11 @@ plugin: oe_corporate_blocks_eu_footer
settings:
id: oe_corporate_blocks_eu_footer
label: 'EU Footer block'
provider: oe_corporate_blocks
label_display: '0'
provider: oe_corporate_blocks
visibility:
oe_whitelabel_helper_current_component_library:
id: oe_whitelabel_helper_current_component_library
component_library: eu
negate: false
context_mapping: { }
component_library: eu
2 changes: 1 addition & 1 deletion config/optional/block.block.oe_whitelabel_eulogin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ plugin: oe_authentication_login_block
settings:
id: oe_authentication_login_block
label: 'EU Login Link Block'
provider: oe_authentication
label_display: '0'
provider: oe_authentication
visibility: { }
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ plugin: 'language_block:language_interface'
settings:
id: 'language_block:language_interface'
label: 'Language switcher (Interface text)'
provider: language
label_display: '0'
provider: language
visibility: { }
4 changes: 2 additions & 2 deletions config/optional/block.block.oe_whitelabel_local_actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ dependencies:
id: oe_whitelabel_local_actions
theme: oe_whitelabel
region: content
weight: 0
weight: -4
provider: null
plugin: local_actions_block
settings:
id: local_actions_block
label: 'Primary admin actions'
provider: core
label_display: '0'
provider: core
visibility: { }
4 changes: 2 additions & 2 deletions config/optional/block.block.oe_whitelabel_local_tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ dependencies:
id: oe_whitelabel_local_tasks
theme: oe_whitelabel
region: content
weight: -1
weight: -5
provider: null
plugin: local_tasks_block
settings:
id: local_tasks_block
label: Tabs
provider: core
label_display: '0'
provider: core
primary: true
secondary: true
visibility: { }
19 changes: 19 additions & 0 deletions config/optional/block.block.oe_whitelabel_main_page_content.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
langcode: en
status: true
dependencies:
module:
- system
theme:
- oe_whitelabel
id: oe_whitelabel_main_page_content
theme: oe_whitelabel
region: content
weight: -2
provider: null
plugin: system_main_block
settings:
id: system_main_block
label: 'Main page content'
label_display: '0'
provider: system
visibility: { }
4 changes: 2 additions & 2 deletions config/optional/block.block.oe_whitelabel_messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ dependencies:
id: oe_whitelabel_messages
theme: oe_whitelabel
region: content
weight: -3
weight: -6
provider: null
plugin: system_messages_block
settings:
id: system_messages_block
label: 'Status messages'
provider: system
label_display: '0'
provider: system
visibility: { }
4 changes: 2 additions & 2 deletions config/optional/block.block.oe_whitelabel_neutral_footer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ plugin: oe_corporate_blocks_neutral_footer
settings:
id: oe_corporate_blocks_neutral_footer
label: 'Neutral Footer block'
provider: oe_whitelabel_helper
label_display: '0'
provider: oe_whitelabel_helper
visibility:
oe_whitelabel_helper_current_component_library:
id: oe_whitelabel_helper_current_component_library
component_library: neutral
negate: false
context_mapping: { }
component_library: neutral
4 changes: 2 additions & 2 deletions config/optional/block.block.oe_whitelabel_search_form.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ plugin: whitelabel_search_block
settings:
id: whitelabel_search_block
label: 'Whitelabel Search Block'
provider: oe_whitelabel_search
label_display: '0'
provider: oe_whitelabel_search
form:
action: '#'
input:
Expand All @@ -26,7 +26,7 @@ settings:
button:
classes: ''
view_options:
enable_autocomplete: false
id: null
display: null
enable_autocomplete: false
visibility: { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: OpenEuropa Whitelabel Multilingual
type: module
description: Adds additional functionality to the Multilingual module
package: OpenEuropa Whitelabel Theme
core_version_requirement: ^9.2
dependencies:
- openeuropa:oe_multilingual
118 changes: 118 additions & 0 deletions modules/oe_whitelabel_multilingual/oe_whitelabel_multilingual.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

/**
* @file
* OE Whitelabel theme Multilingual.
*/

declare(strict_types = 1);

/**
* Implements hook_preprocess_links().
*/
function oe_whitelabel_multilingual_preprocess_links__oe_multilingual_content_language_block(array &$variables): void {
// Generate the label for the unavailable language.
/** @var \Drupal\Core\Language\LanguageInterface[] $languages */
$languages = \Drupal::service('language_manager')->getNativeLanguages();
$variables['unavailable_language'] = $languages[$variables['current_language_id']]->getName();

// Normalize the links to an array of optional languages suitable for the BCL.
$variables['languages'] = [];
foreach ($variables['links'] as $language_code => $link) {
/** @var \Drupal\Core\Url $url */
$url = $link['link']['#url'];
$href = $url
->setOptions($link['link']['#options'])
->setAbsolute(TRUE)
->toString();

$variables['languages'][] = [
'path' => $href,
'hreflang' => $language_code,
'label' => $link['link']['#title'],
'current' => FALSE,
];
}

// Add the current language to the list.
$multilingual_helper = \Drupal::service('oe_multilingual.helper');
$entity = $multilingual_helper->getEntityFromCurrentRoute();
/** @var \Drupal\Core\Entity\EntityInterface $translation */
Copy link
Contributor

Choose a reason for hiding this comment

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

Put a @var doc on the $multilingual_helper service above, then you don't need one here.

$translation = $multilingual_helper->getCurrentLanguageEntityTranslation($entity);
// If we don't have a language id defined yet, the current translation wasn't
// saved, so we don't add it to the list.
if ($translation->language()->getId() !== 'und') {
Copy link
Contributor

Choose a reason for hiding this comment

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

I notice you are using 'und' string literal instead of a constant.
In Drupal 7 I would have told you to use LANGUAGE_NONE.
Now this has changed, see LANGUAGE_NONE changed to LANGUAGE_NOT_SPECIFIED, LANGUAGE_NOT_APPLICABLE and LANGUAGE_MULTIPLE added

Not sure if we also need to support other possible values.
Perhaps safer to just check isset($languages[$current_content_language_id])?

$variables['languages'][] = [
'path' => $translation->toUrl()->setAbsolute(TRUE)->toString(),
'hreflang' => $translation->language()->getId(),
'label' => $languages[$translation->language()->getId()]->getName(),
Copy link
Contributor

Choose a reason for hiding this comment

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

We could introduce a local var for $translation->language()->getId(), to avoid the repeated call, e.g. $current_content_language_id.

We could also check whether $languages[$current_content_language_id] exists.
Would be strange if it does not. Perhaps if a site language is deleted, renamed or disabled, while translations still exist? I don't think it will happen.

'current' => TRUE,
'icon_position' => 'before',
'remove_icon_spacers' => FALSE,
'icon' => [
'name' => 'check-lg',
'path' => $variables['bcl_icon_path'],
'size' => 'xs',
'#attributes' => ['class' => ['me-2']],
],
];
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the scenario where this happens?

Copy link
Contributor

Choose a reason for hiding this comment

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

Btw I was considering to suggest early return pattern here (invert the if to get rid of indentation).
But then we would have to drop the early return when we add the $variables['expandable_id'].
This is why I prefer to use early return only in functions with a single responsibility - which is often not the case for preprocess.
So ok to keep it this way.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also if language is 'und', will the message "This page is not available in ***." make sense for all cases?

I think we should settle for something that works, we have no time to investigate all possible scenarios.
If you say you want to keep the current version, let's go with it.

}

/**
* Implements hook_preprocess_links__language_block().
*/
function oe_whitelabel_multilingual_preprocess_links__language_block(&$variables) {
donquixote marked this conversation as resolved.
Show resolved Hide resolved
$currentLanguage = \Drupal::languageManager()->getCurrentLanguage();
$current_language_id = $currentLanguage->getId();
$language_config_storage = \Drupal::entityTypeManager()->getStorage('configurable_language');
$eu_links = [];
$non_eu_links = [];

foreach ($variables['links'] as $language_code => $link) {
/** @var \Drupal\Core\Url $url */
$url = $link['link']['#url'];
$href = $url
->setOptions($link['link']['#options'])
->setAbsolute(TRUE)
->toString();
$label = $link['link']['#title'];

$link = [
'href' => $href,
'name' => $label,
'id' => 'link_' . $language_code,
'hreflang' => $language_code,
];

if (!empty($current_language_id) && $language_code === $current_language_id) {
$variables['language']['link'] = $link;
$variables['language']['link']['target'] = 'languageModal';
$link['active'] = TRUE;
}

$language_config = $language_config_storage->load($language_code);
$category = $language_config->getThirdPartySetting('oe_multilingual', 'category');

if ($category === 'eu') {
$eu_links[$language_code] = $link;
}
else {
$non_eu_links[$language_code] = $link;
}
}

$variables['language']['modal'] = [
'id' => 'languageModal',
'size' => 'fullscreen',
'header' => [
'title' => t('Select your language'),
'label' => t('Close'),
],
'eu_links' => $eu_links,
'non_eu_links' => $non_eu_links,
'footer' => [
'label' => t('Close'),
],
];
}
17 changes: 9 additions & 8 deletions oe_whitelabel.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ config_devel:
install:
- oe_whitelabel.settings
optional:
- block.block.breadcrumbs
- block.block.corporateeclogoblock
- block.block.ecfooterblock
- block.block.euloginlinkblock.yml
- block.block.languageswitcherinterfacetext
- block.block.oe_whitelabel_branding
- block.block.oe_whitelabel_breadcrumbs
- block.block.oe_whitelabel_ec_corporate_footer
- block.block.oe_whitelabel_eu_corporate_footer
- block.block.oe_whitelabel_eulogin
- block.block.oe_whitelabel_language_switcher
- block.block.oe_whitelabel_local_actions
- block.block.oe_whitelabel_local_tasks
- block.block.oe_whitelabel_main_navigation
- block.block.oe_whitelabel_main_page_content
- block.block.oe_whitelabel_messages
- block.block.oe_whitelabel_page_title
- block.block.whitelabelsearchblock
- cas.settings.yml
- block.block.oe_whitelabel_neutral_footer
- block.block.oe_whitelabel_search_form
Copy link
Contributor

Choose a reason for hiding this comment

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

I found that these entries are already wrong in 1.x.
Also in two other files:

  • oe_whitelabel_starter_event.info.yml (wrong order)
  • oe_whitelabel_starter_news.info.yml (".yml" suffix)

Imo we should have a separate branch to fix all of the entries, and then we rebase.
But I can accept if we want to do this here to move forward.

58 changes: 0 additions & 58 deletions oe_whitelabel.theme
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,6 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\oe_whitelabel_helper\EuropeanUnionLanguages;

/**
* Implements hook__preprocess_links__language_block().
*/
function oe_whitelabel_preprocess_links__language_block(&$variables) {
$currentLanguage = \Drupal::languageManager()->getCurrentLanguage();
$current_language_id = $currentLanguage->getId();
$language_config_storage = \Drupal::entityTypeManager()->getStorage('configurable_language');
$eu_links = [];
$non_eu_links = [];

foreach ($variables['links'] as $language_code => $link) {
/** @var \Drupal\Core\Url $url */
$url = $link['link']['#url'];
$href = $url
->setOptions($link['link']['#options'])
->setAbsolute(TRUE)
->toString();
$label = $link['link']['#title'];

$link = [
'href' => $href,
'name' => $label,
'id' => 'link_' . $language_code,
'hreflang' => $language_code,
];

if (!empty($current_language_id) && $language_code === $current_language_id) {
$variables['language']['link'] = $link;
$variables['language']['link']['target'] = 'languageModal';
$link['active'] = TRUE;
}

$language_config = $language_config_storage->load($language_code);
$category = $language_config->getThirdPartySetting('oe_multilingual', 'category');

if ($category === 'eu') {
$eu_links[$language_code] = $link;
}
else {
$non_eu_links[$language_code] = $link;
}
}

$variables['language']['modal'] = [
'id' => 'languageModal',
'size' => 'fullscreen',
'header' => [
'title' => t('Select your language'),
'label' => t('Close'),
],
'eu_links' => $eu_links,
'non_eu_links' => $non_eu_links,
'footer' => [
'label' => t('Close'),
],
];
}

/**
* Implements hook_form_FORM_ID_alter() for facets_forms.
*/
Expand Down
Loading