CommerceContentEntityBase.php in Commerce Core 8.2
File
src/Entity/CommerceContentEntityBase.php
View source
<?php
namespace Drupal\commerce\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Language\LanguageInterface;
class CommerceContentEntityBase extends ContentEntityBase implements CommerceContentEntityInterface {
public function getTranslatedReferencedEntities($field_name) {
$referenced_entities = $this
->get($field_name)
->referencedEntities();
return $this
->ensureTranslations($referenced_entities);
}
public function getTranslatedReferencedEntity($field_name) {
$referenced_entities = $this
->getTranslatedReferencedEntities($field_name);
$referenced_entity = reset($referenced_entities);
return $referenced_entity ?: NULL;
}
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) {
$entities[$index] = $entity
->hasTranslation($langcode) ? $entity
->getTranslation($langcode) : $entity;
}
return $entities;
}
}