You are here

public function BlazyEntityTrait::getFieldString in Blazy 8

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

2 calls to BlazyEntityTrait::getFieldString()
BlazyEntityReferenceBase::buildElement in src/Dejavu/BlazyEntityReferenceBase.php
Returns item contents.
BlazyEntityReferenceBase::buildStage in src/Dejavu/BlazyEntityReferenceBase.php
Build the main background/stage, image or video.

File

src/Dejavu/BlazyEntityTrait.php, line 18

Class

BlazyEntityTrait
A Trait common for supported entities.

Namespace

Drupal\blazy\Dejavu

Code

public function getFieldString($entity, $field_name = '', $langcode = NULL) {
  $value = '';
  $has_field = !empty($field_name) && isset($entity->{$field_name});
  if (!$has_field) {
    return $value;
  }
  if ($entity
    ->hasTranslation($langcode)) {

    // If the entity has translation, fetch the translated value.
    $values = $entity
      ->getTranslation($langcode)
      ->get($field_name)
      ->getValue();
  }
  else {

    // Entity doesn't have translation, fetch original value.
    $values = $entity
      ->get($field_name)
      ->getValue();
  }
  $value = isset($values[0]['uri']) ? $values[0]['uri'] : (isset($values[0]['value']) ? $values[0]['value'] : '');
  $value = strip_tags($value);
  return trim($value);
}