You are here

class FallbackController in Entity Language Fallback 8

Hierarchy

Expanded class hierarchy of FallbackController

1 string reference to 'FallbackController'
entity_language_fallback.services.yml in ./entity_language_fallback.services.yml
entity_language_fallback.services.yml
1 service uses FallbackController
language_fallback.controller in ./entity_language_fallback.services.yml
Drupal\entity_language_fallback\FallbackController

File

src/FallbackController.php, line 10

Namespace

Drupal\entity_language_fallback
View 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

Namesort descending Modifiers Type Description Overrides
FallbackController::$entityBundleCandidates protected property
FallbackController::$fallback_chain protected property Array of fallback language codes.
FallbackController::$languageManager protected property
FallbackController::ensureFallbackChain protected function Populate internal fallback chain information if necessary.
FallbackController::getEntityFallbackCandidates public function @todo: Consider using @cache.memory service for cache meta-data (8.6+). Overrides FallbackControllerInterface::getEntityFallbackCandidates
FallbackController::getFallbackChain public function Overrides FallbackControllerInterface::getFallbackChain
FallbackController::getTranslation public function Overrides FallbackControllerInterface::getTranslation
FallbackController::getTranslations public function Overrides FallbackControllerInterface::getTranslations
FallbackController::__construct public function TranslationJobHandler constructor.