You are here

public function BlazyEntity::getFieldRenderable in Blazy 7

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

Returns the formatted renderable array of the field.

1 call to BlazyEntity::getFieldRenderable()
BlazyEntity::getFieldTextOrLink in src/BlazyEntity.php
Returns the text or link value of the fields: link, or text.

File

src/BlazyEntity.php, line 168

Class

BlazyEntity
Implements BlazyFormatterInterface.

Namespace

Drupal\blazy

Code

public function getFieldRenderable($entity, $field_name, $settings, $multiple = TRUE) {
  if ($field = field_get_items($entity->targetType, $entity, $field_name)) {

    // If $multiple, use theme_field(). To fetch only the first item, add 0
    // which in turn similar to field_view_value() aka a single output.
    $fields = field_view_field($entity->targetType, $entity, $field_name, $settings['view_mode']);
    $weight = isset($fields['#weight']) ? $fields['#weight'] : 0;

    // Intentionally clean markups as this is not meant for vanilla.
    // Use text format to add extra markups for texts instead.
    if ($multiple) {
      $items = [];
      $entity->_field_view_prepared = FALSE;
      foreach (element_children($fields) as $key) {
        if (!empty($field[$key]['value']) && isset($field[$key]['format'])) {
          $items[] = [
            '#markup' => $this
              ->getFieldString($entity, $field_name, $settings, FALSE),
          ];
        }
        else {
          $items[] = field_view_value($entity->targetType, $entity, $field_name, $field[$key], $settings['view_mode']);
        }
      }
      $items['#weight'] = $weight;
      $entity->_field_view_prepared = TRUE;
      return $items;
    }
    return field_view_value($entity->targetType, $entity, $field_name, $field[0], $settings['view_mode']);
  }
  return [];
}