You are here

public function BlazyFilter::buildImageCaption in Blazy 8.2

Gets the caption if available.

Parameters

array $build: The content array being modified.

object $node: The HTML DOM object.

Overrides BlazyFilterInterface::buildImageCaption

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

File

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

Class

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

Namespace

Drupal\blazy\Plugin\Filter

Code

public function buildImageCaption(array &$build, &$node) {

  // Sanitization was done by Caption filter when arriving here, as
  // otherwise we cannot see this figure, yet provide fallback.
  if ($node->parentNode && $node->parentNode->tagName === 'figure') {
    $caption = $node->parentNode
      ->getElementsByTagName('figcaption');
    $item = $caption->length > 0 && $caption
      ->item(0) ? $caption
      ->item(0) : NULL;
    if ($item && ($text = $item->ownerDocument
      ->saveXML($item))) {
      $settings =& $build['settings'];
      $markup = Xss::filter($text, BlazyDefault::TAGS);
      $build['captions']['alt'] = [
        '#markup' => $markup,
      ];
      if (isset($settings['box_caption']) && $settings['box_caption'] == 'inline') {
        $settings['box_caption'] = $markup;
      }

      // Mark the FIGCAPTION for deletion because the caption will be
      // rendered in the Blazy way.
      $item
        ->setAttribute('class', 'blazy-removed');

      // Marks figures for removal as its contents are moved into grids.
      if ($settings['_grid']) {
        $node->parentNode
          ->setAttribute('class', 'blazy-removed');
      }
    }
  }
}