You are here

public function BlazyEntity::getFieldRenderable in Blazy 8.2

Same name and namespace in other branches
  1. 7 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 202

Class

BlazyEntity
Provides common entity utilities to work with field details.

Namespace

Drupal\blazy

Code

public function getFieldRenderable($entity, $field_name, $view_mode, $multiple = TRUE) {
  if ($entity
    ->hasField($field_name) && !empty($entity->{$field_name}
    ->view($view_mode)[0])) {
    $view = $entity
      ->get($field_name)
      ->view($view_mode);

    // Prevents quickedit to operate here as otherwise JS error.
    // @see 2314185, 2284917, 2160321.
    // @see quickedit_preprocess_field().
    // @todo Remove when it respects plugin annotation.
    $view['#view_mode'] = '_custom';
    $weight = isset($view['#weight']) ? $view['#weight'] : 0;

    // Intentionally clean markups as this is not meant for vanilla.
    if ($multiple) {
      $items = [];
      foreach (Element::children($view) as $key) {
        $items[$key] = $entity
          ->get($field_name)
          ->view($view_mode)[$key];
      }
      $items['#weight'] = $weight;
      return $items;
    }
    return $view[0];
  }
  return [];
}