LanguageApi.php in Little helpers 7
File
src/Locale/LanguageApi.php
View source
<?php
namespace Drupal\little_helpers\Locale;
class LanguageApi {
public function currentLanguage() {
return $GLOBALS['language']->language;
}
public function defaultLanguage() {
return \language_default()->language;
}
public function redirect($path, $language) {
\drupal_goto($path, array(
'language' => $language,
));
}
public function switchLinks($path) {
$links = \language_negotiation_get_switch_links('language', $path);
return $links ? $links->links : NULL;
}
public function checkAccess($path, $langCode) {
if (empty($path)) {
if (module_exists('i18n_variable')) {
$path = \i18n_variable_get('site_frontpage', $langCode, $path);
}
else {
$path = \variable_get('site_frontpage', $path);
}
}
return ($router_item = \menu_get_item($path)) && $router_item['access'];
}
public function languageLinks($languages = NULL, $path = NULL) {
if (!$languages) {
$languages = \language_list();
}
if (!$path) {
$path = \current_path();
}
$currentLanguage = $this
->currentLanguage();
$links = array();
$switchLinks = $this
->switchLinks($path);
foreach ($languages as $code => $language) {
if ($code == $currentLanguage) {
continue;
}
if (isset($switchLinks[$code]) && isset($switchLinks[$code]['href']) && $this
->checkAccess($switchLinks[$code]['href'], $code)) {
$links[$code] = $switchLinks[$code];
}
}
return $links;
}
}