You are here

protected function PhotoswipePreprocessProcessor::getCaption in PhotoSwipe 3.x

Set the caption.

1 call to PhotoswipePreprocessProcessor::getCaption()
PhotoswipePreprocessProcessor::preprocess in src/PhotoswipePreprocessProcessor.php
Preprocess image.

File

src/PhotoswipePreprocessProcessor.php, line 140

Class

PhotoswipePreprocessProcessor
Preprocess photoswipe images.

Namespace

Drupal\photoswipe

Code

protected function getCaption() {
  $settings = $this->imageDTO
    ->getSettings();
  if (isset($settings['photoswipe_caption'])) {
    $caption_setting = $settings['photoswipe_caption'];
    switch ($caption_setting) {
      case 'alt':
        $caption = $this->imageDTO
          ->getAlt();
        break;
      case 'title':
        $caption = $this->imageDTO
          ->getTitle();
        break;
      case 'node_title':
        if (!empty($this->entity->title)) {
          $caption = $this->imageDTO
            ->getEntity()->title->value;
        }
        else {
          $caption = $this->imageDTO
            ->getAlt();
        }
        break;
      case 'custom':
        $entity_type = $this->imageDTO
          ->getEntity()
          ->getEntityTypeId();
        $caption = $this->token
          ->replace($settings['photoswipe_caption_custom'], [
          $entity_type => $this->imageDTO
            ->getEntity(),
          'file' => $this->imageDTO
            ->getItem(),
        ], [
          'clear' => TRUE,
          'langcode' => $this->languageManager
            ->getCurrentLanguage()
            ->getId(),
        ]);
        break;
      default:

        // Assume the user wants to use another node field as the caption.
        $field_view['#view_mode'] = $settings['photoswipe_view_mode'] ? $settings['photoswipe_view_mode'] : 'default';
        if (!isset($entity->{$caption_setting})) {

          // No such field exists we'd better warn and use something reliable.
          $id = $this->imageDTO
            ->getEntity()
            ->id();
          $msg = "'Photoswipe Caption' is unset for field view '@fv' on node: @nid.";
          $this->logger
            ->warning($msg, [
            '@fv' => $field_view['#view_mode'],
            '@nid' => $id,
          ]);

          // Fallback to alt text:
          $caption = $this->imageDTO
            ->getAlt();
          break;
        }
        $field_view = $entity->{$caption_setting}
          ->view();
        $caption = render($field_view);
        break;
    }
  }
  else {
    $caption = $this->imageDTO
      ->getAlt();
  }
  return $caption;
}