DisableLanguageManager.php in Disable language 8
File
src/DisableLanguageManager.php
View source
<?php
namespace Drupal\disable_language;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageDefault;
use Drupal\language\Config\LanguageConfigFactoryOverrideInterface;
use Drupal\language\ConfigurableLanguageManager;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Language\LanguageManager;
class DisableLanguageManager extends ConfigurableLanguageManager {
protected $entityTypeManager;
protected $enabledLanguages;
protected $disabledLanguages;
protected $languageManager;
public function __construct(LanguageDefault $default_language, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, LanguageConfigFactoryOverrideInterface $config_override, RequestStack $request_stack, EntityTypeManagerInterface $entityTypeManager, languageManager $languageManager) {
parent::__construct($default_language, $config_factory, $module_handler, $config_override, $request_stack);
$this->entityTypeManager = $entityTypeManager;
$this->languageManager = $languageManager;
$this->disabledLanguages = [];
$this->enabledLanguages = [];
}
public function getConfigurableLanguages() {
$configurableLanguages = [];
$languages = $this
->getLanguages();
$configManager = $this->entityTypeManager
->getStorage('configurable_language');
$configurableLanguages = $configManager
->loadMultiple(array_keys($languages));
return $configurableLanguages;
}
public function getDisabledLanguages() {
if (empty($this->disabledLanguages)) {
foreach ($this
->getConfigurableLanguages() as $langcode => $configurableLanguage) {
$disabled = $configurableLanguage
->getThirdPartySetting('disable_language', 'disable');
if (isset($disabled) && $disabled == 1) {
$this->disabledLanguages[$langcode] = $configurableLanguage;
}
}
}
return $this->disabledLanguages;
}
public function getEnabledLanguages() {
if (empty($this->enabledLanguages)) {
foreach ($this
->getConfigurableLanguages() as $langcode => $configurableLanguage) {
$disabled = $configurableLanguage
->getThirdPartySetting('disable_language', 'disable');
if (isset($disabled) && $disabled == 0 || empty($disabled)) {
$this->enabledLanguages[$langcode] = $configurableLanguage;
}
}
}
return $this->enabledLanguages;
}
public function isCurrentLanguageDisabled() {
$currentLanguageDisabled = FALSE;
$disabledLangCodes = array_keys($this
->getDisabledLanguages());
if (isset($disabledLangCodes) && in_array($this->languageManager
->getCurrentLanguage()
->getId(), $disabledLangCodes)) {
$currentLanguageDisabled = TRUE;
}
return $currentLanguageDisabled;
}
public function getFirstEnabledLanguage() {
$enabledLanguages = $this
->getEnabledLanguages();
$configurableLanguage = reset($enabledLanguages);
return $this
->getLanguage($configurableLanguage
->getId());
}
public function getFallbackLanguage() {
$redirect_language = FALSE;
$disabledLangCodes = array_keys($this
->getDisabledLanguages());
if (isset($disabledLangCodes) && in_array($this->languageManager
->getCurrentLanguage()
->getId(), $disabledLangCodes)) {
$lang = $this
->getConfigurableLanguages();
$key = $this->languageManager
->getCurrentLanguage()
->getId();
if (array_key_exists($this->languageManager
->getCurrentLanguage()
->getId(), $lang)) {
$redirect_language = $lang[$key]
->getThirdPartySetting('disable_language', 'redirect_language');
}
}
return $redirect_language;
}
}