function disable_language_language_switch_links_alter in Disable language 8
Implements hook_language_switch_links_alter().
We need to filter out the disabled languages from the language selection list.
File
- ./
disable_language.module, line 102 - Contains disable_language.module.
Code
function disable_language_language_switch_links_alter(array &$links, $type, $path) {
$user = Drupal::currentUser();
// Only filter the links if the current user doesn't
// have permission to view disabled language.
if (!$user
->hasPermission('view disabled languages')) {
foreach ($links as $langcode => $link) {
// When te language isn't available in the link object we need to load it
// using the entity storage.
if (empty($link['language']) || !$link['language'] instanceof LanguageInterface) {
$language = \Drupal::entityTypeManager()
->getStorage('configurable_language')
->load($langcode);
}
else {
$language = $link['language'];
}
// If the third party settings exists and the disable
// value is true then remove the item from the links list.
$disabled = $language
->getThirdPartySetting('disable_language', 'disable');
if (isset($disabled) && $disabled == 1) {
unset($links[$langcode]);
}
}
}
}