You are here

public function BlazyStylePluginTrait::getImageRenderable in Blazy 8.2

Same name and namespace in other branches
  1. 8 src/Dejavu/BlazyStylePluginTrait.php \Drupal\blazy\Dejavu\BlazyStylePluginTrait::getImageRenderable()
  2. 7 src/Dejavu/BlazyStylePluginTrait.php \Drupal\blazy\Dejavu\BlazyStylePluginTrait::getImageRenderable()

Returns the modified renderable image_formatter to support lazyload.

1 call to BlazyStylePluginTrait::getImageRenderable()
BlazyStylePluginBase::buildElement in src/Dejavu/BlazyStylePluginBase.php
Returns an individual row/element content.

File

src/Dejavu/BlazyStylePluginTrait.php, line 16

Class

BlazyStylePluginTrait
A Trait common for optional views style plugins.

Namespace

Drupal\blazy\Dejavu

Code

public function getImageRenderable(array &$settings, $row, $index) {
  $image = $this
    ->isImageRenderable($row, $index, $settings['image']);

  /** @var Drupal\image\Plugin\Field\FieldType\ImageItem $item */
  if (empty($image['raw'])) {
    return $image;
  }

  // If the image has #item property, lazyload may work, otherwise skip.
  // This hustle is to lazyload tons of images -- grids, large galleries,
  // gridstack, mason, with multimedia/ lightboxes for free.
  if ($item = $this
    ->getImageItem($image)) {

    // Supports multiple image styles within a single view such as GridStack,
    // else fallbacks to the defined image style if available.
    if (empty($settings['image_style'])) {
      $image_style = isset($image['rendered']['#image_style']) ? $image['rendered']['#image_style'] : '';
      $settings['image_style'] = empty($settings['image_style']) ? $image_style : $settings['image_style'];
    }

    // Converts image formatter for blazy to reduce complexity with CSS
    // background option, and other options, and still lazyload it.
    $theme = isset($image['rendered']['#theme']) ? $image['rendered']['#theme'] : '';
    if (in_array($theme, [
      'blazy',
      'image_formatter',
    ])) {
      $settings['uri'] = Blazy::uri($item);
      $settings['cache_tags'] = isset($image['rendered']['#cache']['tags']) ? $image['rendered']['#cache']['tags'] : [];
      if ($theme == 'blazy') {

        // Pass Blazy field formatter settings into Views style plugin.
        // This allows richer contents such as multimedia/ lightbox for free.
        // Yet, ensures the Views style plugin wins over Blazy formatter,
        // such as with GridStack which may have its own breakpoints.
        $item_settings = array_filter($image['rendered']['#build']['settings']);
        $settings = array_merge($item_settings, array_filter($settings));
      }
      elseif ($theme == 'image_formatter') {

        // Deals with "link to content/image" by formatters.
        $settings['content_url'] = isset($image['rendered']['#url']) ? $image['rendered']['#url'] : '';

        // Prevent images from having absurd height when being lazyloaded.
        // Allows to disables it by _noratio such as enforced CSS background.
        $settings['ratio'] = empty($settings['_noratio']) ? 'fluid' : '';
        if (empty($settings['media_switch']) && !empty($settings['content_url'])) {
          $settings['media_switch'] = 'content';
        }
      }

      // Rebuilds the image for the brand new richer Blazy.
      // With the working Views cache, nothing to worry much.
      $build = [
        'item' => $item,
        'settings' => $settings,
      ];
      $image['rendered'] = $this->blazyManager
        ->getBlazy($build);
    }
  }
  return $image;
}