You are here

public static function Blazy::buildUrl in Blazy 8

Builds URLs, cache tags, and dimensions for individual image.

1 call to Blazy::buildUrl()
Blazy::buildAttributes in src/Blazy.php
Prepares variables for blazy.html.twig templates.

File

src/Blazy.php, line 340

Class

Blazy
Implements BlazyInterface.

Namespace

Drupal\blazy

Code

public static function buildUrl(array &$settings = [], $item = NULL) {

  // Blazy already sets URI, yet set fallback for direct theme_blazy() call.
  if (empty($settings['uri']) && $item) {
    $settings['uri'] = ($entity = $item->entity) && empty($item->uri) ? $entity
      ->getFileUri() : $item->uri;
  }
  if (empty($settings['uri'])) {
    return;
  }

  // Lazyloaded elements expect image URL, not URI.
  if (empty($settings['image_url'])) {
    $settings['image_url'] = file_url_transform_relative(file_create_url($settings['uri']));
  }

  // Sets dimensions.
  // VEF without image style, or image style with crop, may already set these.
  if (empty($settings['width'])) {
    $settings['width'] = $item && isset($item->width) ? $item->width : NULL;
    $settings['height'] = $item && isset($item->height) ? $item->height : NULL;
  }

  // Image style modifier can be multi-style images such as GridStack.
  if (!empty($settings['image_style']) && ($style = ImageStyle::load($settings['image_style']))) {

    // Image URLs, as opposed to URIs, are expected by lazyloaded images.
    $settings['image_url'] = file_url_transform_relative($style
      ->buildUrl($settings['uri']));
    $settings['cache_tags'] = $style
      ->getCacheTags();

    // Only re-calculate dimensions if not cropped, nor already set.
    if (empty($settings['_dimensions'])) {
      $style
        ->transformDimensions($settings, $settings['uri']);
    }
  }
}