You are here

public function BlazyStyleBaseTrait::getFirstImage in Blazy 8

Same name and namespace in other branches
  1. 8.2 src/Dejavu/BlazyStyleBaseTrait.php \Drupal\blazy\Dejavu\BlazyStyleBaseTrait::getFirstImage()
  2. 7 src/Dejavu/BlazyStyleBaseTrait.php \Drupal\blazy\Dejavu\BlazyStyleBaseTrait::getFirstImage()

Returns the first Blazy formatter found.

File

src/Dejavu/BlazyStyleBaseTrait.php, line 96

Class

BlazyStyleBaseTrait
A Trait common for optional views style plugins.

Namespace

Drupal\blazy\Dejavu

Code

public function getFirstImage($row) {
  if (!isset($this->firstImage)) {
    $rendered = [];
    if ($row && isset($row->_entity) && ($fields = $row->_entity
      ->getFields(FALSE))) {
      foreach ($fields as $field) {
        if ($field
          ->getFieldDefinition()
          ->getFieldStorageDefinition()
          ->getSetting('target_type') == 'media') {
          $name = $field
            ->getName();
          break;
        }
        if ($field
          ->first() instanceof ImageItem) {
          $name = $field
            ->getName();
          break;
        }
      }
      if (isset($name) && ($rendered = $this
        ->getFieldRenderable($row, 0, $name))) {
        if (is_array($rendered) && isset($rendered['rendered']) && !$rendered['rendered'] instanceof Markup) {
          $rendered = isset($rendered['rendered']['#build']) ? $rendered['rendered']['#build'] : [];
        }
      }
    }
    $this->firstImage = $rendered;
  }
  return $this->firstImage;
}