class FallbackController in Entity Language Fallback 8
Hierarchy
- class \Drupal\entity_language_fallback\FallbackController implements FallbackControllerInterface
Expanded class hierarchy of FallbackController
1 string reference to 'FallbackController'
1 service uses FallbackController
File
- src/
FallbackController.php, line 10
Namespace
Drupal\entity_language_fallbackView source
class FallbackController implements FallbackControllerInterface {
/**
* @var string[]
*
* Array of fallback language codes.
*/
protected $fallback_chain;
protected $entityBundleCandidates;
/**
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* TranslationJobHandler constructor.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* An instance of the Language management service.
* @param $entityTypeManager
* An instance of entity type manager service.
*/
public function __construct(LanguageManagerInterface $languageManager, EntityTypeManagerInterface $entityTypeManager) {
$this->languageManager = $languageManager;
$this->entityTypeManager = $entityTypeManager;
$this->fallback_chain = [];
$this->entityBundleCandidates = [];
}
/**
* {@inheritdoc}
*/
public function getFallbackChain($lang_code) {
$this
->ensureFallbackChain($lang_code);
return $this->fallback_chain[$lang_code];
}
/**
* {@inheritdoc}
*
* @todo: Consider using @cache.memory service for cache meta-data (8.6+).
*/
public function getEntityFallbackCandidates(ContentEntityInterface $entity, $language_code) {
// Result of this can change by entity + bundle combination.
$cid = "{$entity->getEntityType()->id()}:{$entity->bundle()}";
if (isset($this->entityBundleCandidates[$cid])) {
return $this->entityBundleCandidates[$cid];
}
// Generate if the value is missing.
$candidates = [];
if ($entity
->isTranslatable()) {
$candidates[$language_code] = $language_code;
foreach ($this
->getFallbackChain($language_code) as $fallback_langcode) {
if (!(empty($fallback_langcode) || isset($candidates[$fallback_langcode]))) {
$candidates[$fallback_langcode] = $fallback_langcode;
}
}
}
$this->entityBundleCandidates[$cid] = $candidates;
return $candidates;
}
/**
* {@inheritdoc}
*/
public function getTranslation($lang_code, ContentEntityInterface $entity) {
$this
->ensureFallbackChain($lang_code);
foreach ($this->fallback_chain[$lang_code] as $candidate) {
if ($entity
->hasTranslation($candidate)) {
return $entity
->getTranslation($candidate);
}
}
return FALSE;
}
/**
* Populate internal fallback chain information if necessary.
*/
protected function ensureFallbackChain($lang_code) {
if (isset($this->fallback_chain[$lang_code])) {
return;
}
$this->fallback_chain[$lang_code] = ($language = ConfigurableLanguage::load($lang_code)) ? $language
->getThirdPartySetting('entity_language_fallback', 'fallback_langcodes', []) : [];
}
/**
* {@inheritdoc}
*/
public function getTranslations(ContentEntityInterface $entity) {
$translations = [];
foreach ($this->languageManager
->getLanguages() as $langcode => $language) {
if ($entity
->hasTranslation($langcode)) {
$translations[$langcode] = $entity
->getTranslation($langcode);
}
elseif ($fallback = $this
->getTranslation($langcode, $entity)) {
$translations[$langcode] = $fallback;
}
}
return $translations;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FallbackController:: |
protected | property | ||
FallbackController:: |
protected | property | Array of fallback language codes. | |
FallbackController:: |
protected | property | ||
FallbackController:: |
protected | function | Populate internal fallback chain information if necessary. | |
FallbackController:: |
public | function |
@todo: Consider using @cache.memory service for cache meta-data (8.6+). Overrides FallbackControllerInterface:: |
|
FallbackController:: |
public | function |
Overrides FallbackControllerInterface:: |
|
FallbackController:: |
public | function |
Overrides FallbackControllerInterface:: |
|
FallbackController:: |
public | function |
Overrides FallbackControllerInterface:: |
|
FallbackController:: |
public | function | TranslationJobHandler constructor. |