You are here

private static function BlazyLightbox::buildCaptions in Blazy 8.2

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

Builds lightbox captions.

Parameters

object|mixed $item: The \Drupal\image\Plugin\Field\FieldType\ImageItem item.

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 194

Class

BlazyLightbox
Provides lightbox utilities.

Namespace

Drupal\blazy

Code

private 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 = '';
  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 = $item
        ->getEntity()) ? $entity
        ->label() : '';
      break;
    case 'custom':
      $caption = '';
      if (!empty($settings['box_caption_custom']) && ($entity = $item
        ->getEntity())) {
        $options = [
          'clear' => TRUE,
        ];
        $caption = \Drupal::token()
          ->replace($settings['box_caption_custom'], [
          $entity
            ->getEntityTypeId() => $entity,
          'file' => $item,
        ], $options);

        // 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;
    default:
      $caption = $settings['box_caption'];
  }
  return empty($caption) ? [] : [
    '#markup' => Xss::filter($caption, BlazyDefault::TAGS),
  ];
}