You are here

protected function CommerceContentEntityBase::ensureTranslations in Commerce Core 8.2

Ensures entities are in the current entity's language, if possible.

Parameters

\Drupal\Core\Entity\ContentEntityInterface[] $entities: The entities to process.

Return value

\Drupal\Core\Entity\ContentEntityInterface[] The processed entities.

2 calls to CommerceContentEntityBase::ensureTranslations()
CommerceContentEntityBase::getTranslatedReferencedEntities in src/Entity/CommerceContentEntityBase.php
Gets the translations of an entity reference field.
ProductVariation::getAttributeValues in modules/product/src/Entity/ProductVariation.php
Gets the attribute values.

File

src/Entity/CommerceContentEntityBase.php, line 39

Class

CommerceContentEntityBase
Provides the base class for Commerce content entities.

Namespace

Drupal\commerce\Entity

Code

protected function ensureTranslations(array $entities) {
  if ($this
    ->isTranslatable()) {
    $langcode = $this
      ->language()
      ->getId();
  }
  else {
    $langcode = $this
      ->languageManager()
      ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
      ->getId();
  }
  foreach ($entities as $index => $entity) {

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entities[$index] = $entity
      ->hasTranslation($langcode) ? $entity
      ->getTranslation($langcode) : $entity;
  }
  return $entities;
}