You are here

public function BlazyManagerBase::buildDataBlazy in Blazy 7

To be deprecated method.

@todo deprecate for BlazyBreakpoint::buildDataBlazy() at blazy:7.x-2.0.

2 calls to BlazyManagerBase::buildDataBlazy()
BlazyFormatter::setImageDimensions in src/BlazyFormatter.php
Sets dimensions once to reduce method calls, if image style contains crop.
BlazyManagerBase::isBlazy in src/BlazyManagerBase.php
Checks for Blazy formatter such as from within a Views style plugin.

File

src/BlazyManagerBase.php, line 354

Class

BlazyManagerBase
Implements BlazyManagerInterface.

Namespace

Drupal\blazy

Code

public function buildDataBlazy(array &$settings, $item = NULL) {

  // Identify that Blazy can be activated by breakpoints, regardless results.
  $settings['blazy'] = TRUE;

  // Bail out if already defined at BlazyFormatter::setImageDimensions().
  // Blazy doesn't always deal with image formatters, see self::isBlazy().
  if (!empty($settings['blazy_data'])) {
    return;
  }

  // May be set at BlazyFormatter::setImageDimensions() if using formatters,
  // yet not set from non-formatters like views fields, see self::isBlazy().
  Blazy::imageDimensions($settings, $item, TRUE);
  $styles = [];
  $end = end($settings['breakpoints']);

  // Check for cropped images at the 5 given styles before any hard work
  // Ok as run once at the top container regardless of thousand of images.
  foreach ($settings['breakpoints'] as $key => $breakpoint) {
    if ($this
      ->isCrop($breakpoint['image_style'])) {
      $styles[$key] = TRUE;
    }
  }

  // Bail out if not all images are cropped at all breakpoints.
  // The site builder just don't read the performance tips section.
  if (count($styles) != count($settings['breakpoints'])) {
    return;
  }

  // We have all images cropped here.
  $json = $sources = [];
  foreach ($settings['breakpoints'] as $key => $breakpoint) {
    if ($width = Blazy::widthFromDescriptors($breakpoint['width'])) {

      // If contains crop, sets dimension once, and let all images inherit.
      if (!empty($settings['ratio'])) {
        $dim = Blazy::transformDimensions($breakpoint['image_style'], $item);
        $padding = round($dim['height'] / $dim['width'] * 100, 2);
        $json['dimensions'][$width] = $padding;

        // Only set padding-bottom for the last breakpoint to avoid FOUC.
        if ($end['width'] == $breakpoint['width']) {
          $settings['padding_bottom'] = $padding;
        }
      }

      // If BG, provide [data-src-BREAKPOINT], regardless uri or ratio.
      // @todo if (!empty($settings['background'])) {
      // @todo   $sources[] = ['width' => (int) $width, 'src' => 'data-src-' . $key];
      // @todo }
    }
  }

  // As of Blazy v1.6.0 applied to BG only.
  if ($sources) {
    $json['breakpoints'] = $sources;
  }

  // Supported modules can add blazy_data as [data-blazy] to the container.
  // This also informs individual images to not work with dimensions any more
  // as _all_ breakpoint image styles contain 'crop'.
  if ($json) {
    $settings['blazy_data'] = $json;
  }
}