You are here

public static function BlazyLightbox::buildCaptions in Blazy 7

Same name and namespace in other branches
  1. 8.2 src/BlazyLightbox.php \Drupal\blazy\BlazyLightbox::buildCaptions()
  2. 8 src/BlazyLightbox.php \Drupal\blazy\BlazyLightbox::buildCaptions()

Builds lightbox captions.

Parameters

object|mixed $item: The \Drupal\image\Plugin\Field\FieldType\ImageItem item, or array when dealing with Video Embed Field.

array $settings: The settings to work with.

Return value

array The renderable array of caption, or empty array.

1 call to BlazyLightbox::buildCaptions()
BlazyLightbox::build in src/BlazyLightbox.php
Gets media switch elements: all lightboxes, not content, nor iframe.

File

src/BlazyLightbox.php, line 122

Class

BlazyLightbox
Provides lightbox utilities.

Namespace

Drupal\blazy

Code

public static function buildCaptions($item, array $settings = []) {
  $title = empty($item->title) ? '' : $item->title;
  $alt = empty($item->alt) ? '' : $item->alt;
  $delta = empty($settings['delta']) ? 0 : $settings['delta'];
  $caption = '';
  $entity = $entity_type = NULL;
  if (!empty($settings['entity'])) {
    $entity_type = $settings['entity_type_id'];
    $entity = $settings['entity'];
    unset($settings['entity']);
  }
  switch ($settings['box_caption']) {
    case 'auto':
      $caption = $alt ?: $title;
      break;
    case 'alt':
      $caption = $alt;
      break;
    case 'title':
      $caption = $title;
      break;
    case 'alt_title':
    case 'title_alt':
      $alt = $alt ? '<p>' . $alt . '</p>' : '';
      $title = $title ? '<h2>' . $title . '</h2>' : '';
      $caption = $settings['box_caption'] == 'alt_title' ? $alt . $title : $title . $alt;
      break;
    case 'entity_title':
      $caption = entity_label($entity_type, $entity);
      break;
    case 'custom':
      if (!empty($settings['box_caption_custom'])) {
        $caption = token_replace($settings['box_caption_custom'], [
          $entity_type => $entity,
          'file' => (object) $item,
        ], [
          'clear' => TRUE,
        ]);

        // Checks for multi-value text fields, and maps its delta to image.
        if (!empty($caption) && strpos($caption, ", <p>") !== FALSE) {
          $caption = str_replace(", <p>", '| <p>', $caption);
          $captions = explode("|", $caption);
          $caption = isset($captions[$delta]) ? $captions[$delta] : '';
        }
      }
      break;
  }
  return empty($caption) ? [] : [
    '#markup' => filter_xss($caption, BlazyDefault::TAGS),
  ];
}