You are here

public function BlazyEntityReferenceBase::getCaption in Blazy 8

Same name and namespace in other branches
  1. 8.2 src/Dejavu/BlazyEntityReferenceBase.php \Drupal\blazy\Dejavu\BlazyEntityReferenceBase::getCaption()

Builds slide captions with possible multi-value fields.

1 call to BlazyEntityReferenceBase::getCaption()
BlazyEntityReferenceBase::buildElement in src/Dejavu/BlazyEntityReferenceBase.php
Returns item contents.

File

src/Dejavu/BlazyEntityReferenceBase.php, line 86

Class

BlazyEntityReferenceBase
Base class for entity reference formatters with field details.

Namespace

Drupal\blazy\Dejavu

Code

public function getCaption(array &$element, $entity, $langcode) {
  $settings = $element['settings'];
  $view_mode = $settings['view_mode'];

  // Title can be plain text, or link field.
  if (!empty($settings['title'])) {
    $field_title = $settings['title'];
    if (isset($entity->{$field_title})) {
      if ($entity
        ->hasTranslation($langcode)) {

        // If the entity has translation, fetch the translated value.
        $title = $entity
          ->getTranslation($langcode)
          ->get($field_title)
          ->getValue();
      }
      else {

        // Entity doesn't have translation, fetch original value.
        $title = $entity
          ->get($field_title)
          ->getValue();
      }
      if (!empty($title[0]['value']) && !isset($title[0]['uri'])) {

        // Prevents HTML-filter-enabled text from having bad markups (h2 > p),
        // except for a few reasonable tags acceptable within H2 tag.
        $element['caption']['title']['#markup'] = strip_tags($title[0]['value'], '<a><strong><em><span><small>');
      }
      elseif (isset($title[0]['uri']) && !empty($title[0]['title'])) {
        $element['caption']['title'] = $this
          ->getFieldRenderable($entity, $field_title, $view_mode)[0];
      }
    }
  }

  // Other caption fields, if so configured.
  if (!empty($settings['caption'])) {
    $caption_items = [];
    foreach ($settings['caption'] as $i => $field_caption) {
      if (!isset($entity->{$field_caption})) {
        continue;
      }
      $caption_items[$i] = $this
        ->getFieldRenderable($entity, $field_caption, $view_mode);
    }
    if ($caption_items) {
      $element['caption']['data'] = $caption_items;
    }
  }

  // Link, if so configured.
  if (!empty($settings['link'])) {
    $field_link = $settings['link'];
    if (isset($entity->{$field_link})) {
      $links = $this
        ->getFieldRenderable($entity, $field_link, $view_mode);

      // Only simplify markups for known formatters registered by link.module.
      if ($links && isset($links['#formatter']) && in_array($links['#formatter'], [
        'link',
      ])) {
        $links = [];
        foreach ($entity->{$field_link} as $i => $link) {
          $links[$i] = $link
            ->view($view_mode);
        }
      }
      $element['caption']['link'] = $links;
    }
  }
  if (!empty($settings['overlay'])) {
    $element['caption']['overlay'] = $this
      ->getOverlay($settings, $entity, $langcode);
  }
}