2019-01-13 06:47:18 +00:00
|
|
|
<?php
|
|
|
|
|
2019-01-22 18:05:38 +00:00
|
|
|
use Drupal\block\Entity\Block;
|
|
|
|
use Drupal\Core\Entity\EntityInterface;
|
|
|
|
use Drupal\Core\Url;
|
2019-01-13 06:47:18 +00:00
|
|
|
|
|
|
|
/**
|
2019-01-22 05:48:06 +00:00
|
|
|
* Implements hook_preprocess_region__footer().
|
2019-01-13 06:47:18 +00:00
|
|
|
*/
|
2019-01-22 05:48:06 +00:00
|
|
|
function agarica_preprocess_region__footer(&$variables) {
|
|
|
|
$block = Block::load('languageswitcher');
|
|
|
|
$block_content = \Drupal::entityTypeManager()
|
|
|
|
->getViewBuilder('block')
|
|
|
|
->view($block);
|
|
|
|
$variables['language_switcher'] = $block_content;
|
2019-01-22 18:05:38 +00:00
|
|
|
$variables['language_switcher_link'] = agarica_languague_switch_link();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a link to switch to another language.
|
|
|
|
*/
|
|
|
|
function agarica_languague_switch_link() {
|
|
|
|
$derivative_id = 'language_interface';
|
|
|
|
$route = \Drupal::getContainer()->get('path.matcher')->isFrontPage() ? '<front>' : '<current>';
|
|
|
|
$current_language = \Drupal::languageManager()->getCurrentLanguage($derivative_id)->getId();
|
|
|
|
$links = \Drupal::languageManager()->getLanguageSwitchLinks($derivative_id, Url::fromRoute($route))->links;
|
|
|
|
$links['en']['title'] = 'Browser this page on English.';
|
|
|
|
$links['es']['title'] = 'Consulta esta pagina en Español';
|
|
|
|
$lid = ($current_language == 'en') ? 'es' : 'en';
|
|
|
|
// Remove current language from the links
|
|
|
|
unset($links[$current_language]);
|
|
|
|
|
|
|
|
// Check if the current path display an entity.
|
|
|
|
$entity = FALSE;
|
|
|
|
foreach (\Drupal::routeMatch()->getParameters() as $param) {
|
|
|
|
if ($param instanceof EntityInterface) {
|
|
|
|
$entity[] = $param;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove links if the content hasnt' been translated yet.
|
|
|
|
if ($entity) {
|
|
|
|
$has_translation = (method_exists($entity[0], 'getTranslationStatus')) ? $entity[0]->getTranslationStatus($lid) : FALSE;
|
|
|
|
$this_translation = ($has_translation && method_exists($entity[0], 'getTranslation')) ? $entity[0]->getTranslation($lid) : FALSE;
|
|
|
|
$access_translation = ($this_translation && method_exists($this_translation, 'access') && $this_translation->access('view')) ? TRUE : FALSE;
|
|
|
|
if (!$this_translation || !$access_translation || $current_language == $lid) {
|
|
|
|
unset($links[$lid]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$links_render_array = [];
|
|
|
|
// Transform the links to arrays that can be renderer.
|
|
|
|
foreach ($links as $language => $link) {
|
|
|
|
$links_render_array[] = [
|
|
|
|
'#type' => 'link',
|
|
|
|
'#url' => $link['url'],
|
|
|
|
'#title' => $link['title'],
|
|
|
|
'#options' => [
|
|
|
|
'language' => $link['language'],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $links_render_array;
|
2019-01-13 06:47:18 +00:00
|
|
|
}
|