You are here

public function BlazyStyleBaseTrait::getFirstImage in Blazy 8.2

Same name and namespace in other branches
  1. 8 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, to save image dimensions once.

Given 100 images on a page, Blazy will call ImageStyle::transformDimensions() once rather than 100 times and let the 100 images inherit it as long as the image style has CROP in the name.

2 calls to BlazyStyleBaseTrait::getFirstImage()
BlazyViews::render in src/Plugin/views/style/BlazyViews.php
Overrides StylePluginBase::render().
BlazyViewsTest::render in tests/modules/blazy_test/src/Plugin/views/style/BlazyViewsTest.php
Overrides StylePluginBase::render().

File

src/Dejavu/BlazyStyleBaseTrait.php, line 110

Class

BlazyStyleBaseTrait
A Trait common for optional views style plugins.

Namespace

Drupal\blazy\Dejavu

Code

public function getFirstImage($row) {
  if (!isset($this->firstImage)) {

    // Fixed for Undefined property: Drupal\views\ViewExecutable::$row_index
    // by Drupal\views\Plugin\views\field\EntityField->prepareItemsByDelta.
    if (!isset($this->view->row_index)) {
      $this->view->row_index = 0;
    }
    $rendered = [];
    if ($row && ($render = $this->view->rowPlugin
      ->render($row))) {
      if (isset($render['#view']->field) && ($fields = $render['#view']->field)) {
        foreach ($fields as $field) {
          $options = isset($field->options) ? $field->options : [];
          if (!isset($options['type'])) {
            continue;
          }
          if (!empty($options['field']) && isset($options['settings']['media_switch']) && strpos($options['type'], 'blazy') !== FALSE) {
            $name = $options['field'];
          }
        }
        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;
}