You are here

private function FieldBlock::getTranslatedFieldFromEntity in Field as Block 8.2

Ensure that the field gets correctly translated into the current language

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity:

Return value

\Drupal\Core\Field\FieldItemListInterface

File

src/Plugin/Block/FieldBlock.php, line 404

Class

FieldBlock
Provides a fieldblock.

Namespace

Drupal\fieldblock\Plugin\Block

Code

private function getTranslatedFieldFromEntity(ContentEntityInterface $entity) {
  $language = $this->languageManager
    ->getCurrentLanguage()
    ->getId();
  $field = $entity
    ->get($this->configuration['field_name']);
  if ($entity
    ->hasTranslation($language)) {
    $translatedEntity = $entity
      ->getTranslation($language);
    $adapter = EntityAdapter::createFromEntity($translatedEntity);
    $field
      ->setContext($this->configuration['field_name'], $adapter);
  }
  return $field;
}