You are here

private function BlazyManager::buildImage in Blazy 7

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

Build out image, or anything related, including cache, CSS background, etc.

1 call to BlazyManager::buildImage()
BlazyManager::buildMedia in src/BlazyManager.php
Build out (Responsive) image.

File

src/BlazyManager.php, line 236

Class

BlazyManager
Implements a public facing blazy manager.

Namespace

Drupal\blazy

Code

private function buildImage(array &$attributes, array &$item_attributes, array &$settings, $item = NULL) {

  // Aspect ratio to fix layout reflow with lazyloaded images responsively.
  // This is outside 'lazy' to allow non-lazyloaded iframes use this too.
  $settings['ratio'] = empty($settings['width']) ? '' : $settings['ratio'];
  if ($settings['ratio']) {
    Blazy::aspectRatioAttributes($attributes, $settings);
  }

  // Supports both lazyloaded or regular image.
  $item_attributes['class'][] = 'media__element';

  // Overrides lazy with blazy for explicit call to reduce another param.
  if (!empty($settings['blazy'])) {
    $settings['lazy'] = 'blazy';
  }
  if ($settings['lazy']) {
    $item_attributes['src'] = empty($settings['placeholder']) ? Blazy::PLACEHOLDER : $settings['placeholder'];
    if ($settings['use_loading']) {
      $attributes['class'][] = 'media--loading';
    }

    // Attach data attributes to either IMG tag, or DIV as background.
    if ($settings['background']) {
      Blazy::lazyAttributes($attributes, $settings);
      Blazy::buildBreakpointAttributes($attributes, $settings, $item);
      $attributes['class'][] = 'b-bg media--background';
      if (!empty($settings['urls'])) {
        $attributes['data-backgrounds'] = drupal_json_encode($settings['urls']);
      }
    }
    else {
      Blazy::lazyAttributes($item_attributes, $settings);
      Blazy::buildBreakpointAttributes($item_attributes, $settings, $item);
    }

    // Multi-breakpoint aspect ratio only applies if lazyloaded.
    if (!empty($settings['blazy_data']['dimensions'])) {
      $attributes['data-dimensions'] = drupal_json_encode($settings['blazy_data']['dimensions']);
    }
  }
}