flags_language.module in Flags 8
File
flags_language/flags_language.moduleView source
<?php
use Drupal\flags\Mapping\FlagMappingInterface;
use Drupal\Core\Block\BlockPluginInterface;
/**
* Implements hook_language_switch_links_alter().
*
* Implemented to add flags to the language switcher links.
*
* @param array $links
* @param string $type
* @param string $path
*/
function flags_language_language_switch_links_alter(array &$links, $type, $path) {
/** @var FlagMappingInterface $mapper */
$mapper = \Drupal::service('flags.mapping.language');
foreach ($links as $langCode => &$link) {
$title = $link['title'];
// If title is a string, then we turn it into renderable array.
// Otherwise it probably already is a renderable array.
if (is_string($title)) {
$title = array(
'#markup' => $link['title'],
);
}
$link['title'] = array(
'flag' => array(
'#theme' => 'flags',
'#code' => $mapper
->map($langCode),
'#source' => 'language',
),
'title' => $title,
);
}
}
/**
* Implements hook_block_view_BASE_BLOCK_ID_alter().
*
* Implemented to attach flags CSS to the language switcher block.
*
* @param array $build
* @param BlockPluginInterface $block
*/
function flags_language_block_view_language_block_alter(array &$build, BlockPluginInterface $block) {
$build['#attached']['library'][] = 'flags/flags';
}
function flags_language_field_widget_info_alter(array &$info) {
// Allow to use widget language_select_menu only when select_icons enabled.
if (\Drupal::moduleHandler()
->moduleExists('select_icons')) {
$info['language_select_menu']['field_types'][] = 'language';
}
}