You are here

public function BlazyEntityReferenceBase::getCaption in Blazy 8.2

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

Builds slide captions with possible multi-value fields.

Overrides BlazyEntityMediaBase::getCaption

File

src/Dejavu/BlazyEntityReferenceBase.php, line 49

Class

BlazyEntityReferenceBase
Base class for all entity reference formatters with field details.

Namespace

Drupal\blazy\Dejavu

Code

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

  // Title can be plain text, or link field.
  if (!empty($settings['title'])) {
    if (isset($entity->{$settings['title']})) {
      $element['caption']['title'] = $this
        ->blazyEntity()
        ->getFieldTextOrLink($entity, $settings['title'], $settings);
    }
    elseif (isset($element['item']) && ($item = $element['item'])) {
      if ($settings['title'] == 'title' && ($caption = trim($item
        ->get('title')
        ->getString()))) {
        $markup = Xss::filter($caption, BlazyDefault::TAGS);
        $element['caption']['title'] = [
          '#markup' => $markup,
        ];
      }
    }
  }

  // Link, if so configured.
  if (!empty($settings['link']) && isset($entity->{$settings['link']})) {
    $links = $this
      ->blazyEntity()
      ->getFieldRenderable($entity, $settings['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->{$settings['link']} as $link) {
        $links[] = $link
          ->view($view_mode);
      }
    }
    $element['caption']['link'] = $links;
  }

  // Overlay, if so configured.
  if (!empty($settings['overlay']) && isset($entity->{$settings['overlay']})) {
    $element['caption']['overlay'] = $this
      ->getOverlay($settings, $entity, $langcode);
  }
}