You are here

public function BlazyEntity::getFieldValue in Blazy 7

Same name and namespace in other branches
  1. 8.2 src/BlazyEntity.php \Drupal\blazy\BlazyEntity::getFieldValue()

Returns the string value of the fields: link, or text.

Watch out the Entity output vs. file entity via field_get_items().

2 calls to BlazyEntity::getFieldValue()
BlazyEntity::getFieldString in src/BlazyEntity.php
Returns the string value of the fields: link, or text.
BlazyEntity::getFieldTextOrLink in src/BlazyEntity.php
Returns the text or link value of the fields: link, or text.

File

src/BlazyEntity.php, line 107

Class

BlazyEntity
Implements BlazyFormatterInterface.

Namespace

Drupal\blazy

Code

public function getFieldValue($entity, $field_name, $settings) {
  if ($entity instanceof \Entity) {

    // We have 3 possible outputs for link or text fields here:
    // 1. string, 2. $array['value'], 3. $array[0]['url']
    // Note! We have no field_get_items()-like output: $array[0]['value'].
    // If the entity has translation, fetch the translated value instead.
    $translated = $entity
      ->wrapper()
      ->language($settings['langcode'])->{$field_name}
      ->value();
    return $translated ?: $entity
      ->wrapper()->{$field_name}
      ->value();
  }

  // File entity is not based on \Entity, and here comes the complication.
  return field_get_items($entity->targetType, $entity, $field_name);
}