You are here

public function BlazyFilter::buildImageItem in Blazy 8.2

Same name and namespace in other branches
  1. 7 src/Plugin/Filter/BlazyFilter.php \Drupal\blazy\Plugin\Filter\BlazyFilter::buildImageItem()

Returns the faked image item for the image, uploaded or hard-coded.

Parameters

array $build: The content array being modified.

object $node: The HTML DOM object.

Overrides BlazyFilterInterface::buildImageItem

1 call to BlazyFilter::buildImageItem()
BlazyFilter::process in src/Plugin/Filter/BlazyFilter.php
Performs the filter processing.

File

src/Plugin/Filter/BlazyFilter.php, line 332

Class

BlazyFilter
Provides a filter to lazyload image, or iframe elements.

Namespace

Drupal\blazy\Plugin\Filter

Code

public function buildImageItem(array &$build, &$node) {
  $settings =& $build['settings'];
  $item = NULL;

  // Checks if we have a valid file entity, not hard-coded image URL.
  if ($src = $node
    ->getAttribute('src')) {

    // Prevents data URI from screwing up.
    $data_uri = mb_substr($src, 0, 10) === 'data:image';
    if (!$data_uri) {

      // If starts with 2 slashes, it is always external.
      if (mb_substr($src, 0, 2) === '//') {

        // We need to query stored SRC, https is enforced.
        $src = 'https:' . $src;
      }
      if ($node->tagName == 'img') {
        $item = $this
          ->getImageItemFromImageSrc($settings, $node, $src);
      }
      elseif ($node->tagName == 'iframe') {
        try {

          // Prevents invalid video URL (404, etc.) from screwing up.
          $item = $this
            ->getImageItemFromIframeSrc($settings, $node, $src);
        } catch (\Exception $ignore) {

          // Do nothing, likely local work without internet, or the site is
          // down. No need to be chatty on this.
        }
      }
    }
  }
  if ($item) {
    $item->alt = $node
      ->getAttribute('alt') ?: (isset($item->alt) ? $item->alt : '');
    $item->title = $node
      ->getAttribute('title') ?: (isset($item->title) ? $item->title : '');

    // Supports hard-coded image url without file API.
    if (!empty($item->uri) && empty($item->width)) {
      if ($data = @getimagesize($item->uri)) {
        list($item->width, $item->height) = $data;
      }
    }
  }

  // Responsive image with aspect ratio requires an extra container to work
  // with Align/ Caption images filters.
  $build['media_attributes']['class'] = [
    'media-wrapper',
    'media-wrapper--blazy',
  ];

  // Copy all attributes of the original node to the item_attributes.
  if ($node->attributes->length) {
    foreach ($node->attributes as $attribute) {
      if ($attribute->nodeName == 'src') {
        continue;
      }

      // Move classes (align-BLAH,etc) to Blazy container, not image so to
      // work with alignments and aspect ratio. Sanitization is performed at
      // BlazyManager::prepareBlazy() to avoid double escapes.
      if ($attribute->nodeName == 'class') {
        $build['media_attributes']['class'][] = $attribute->nodeValue;
      }
      elseif (!isset($item->target_id)) {
        $build['item_attributes'][$attribute->nodeName] = $attribute->nodeValue;
      }
    }
    $build['media_attributes']['class'] = array_unique($build['media_attributes']['class']);
  }
  $build['item'] = $item;
}