You are here

bootstrap_languages.module in Bootstrap Languages 8

Same filename and directory in other branches
  1. 7 bootstrap_languages.module

Provides a Bootstrap dropdown button to switch between available languages.

File

bootstrap_languages.module
View source
<?php

/**
 * @file
 * Provides a Bootstrap dropdown button to switch between available languages.
 */
use Drupal\Component\Utility\NestedArray;

/**
 * Implements hook_theme().
 */
function bootstrap_languages_theme() {
  $theme['links__bootstrap_language_block'] = [
    'base hook' => 'links',
  ];
  return $theme;
}

/**
 * Implements hook_preprocess_HOOK().
 *
 * Preprocess for links--bootstrap-language-block.html.twig.
 */
function bootstrap_languages_preprocess_links__bootstrap_language_block(&$vars) {
  $vars['current_language'] = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  $vars['languages'] = [];
  foreach ($vars['links'] as $lang_key => $link) {

    /** @var \Drupal\Core\Url $url */
    $url = $link['link']['#url'];
    $url_options = NestedArray::mergeDeep($url
      ->getOptions(), $link['link']['#options']);
    $vars['languages'][$lang_key] = $url
      ->setOptions($url_options);
  }
}