You are here

public function BlazyEntityReferenceBase::getCaption in Blazy 7

Builds slide captions with possible multi-value fields.

1 call to BlazyEntityReferenceBase::getCaption()
BlazyEntityReferenceBase::buildElement in src/Plugin/Field/FieldFormatter/BlazyEntityReferenceBase.php
Build individual item contents.

File

src/Plugin/Field/FieldFormatter/BlazyEntityReferenceBase.php, line 127

Class

BlazyEntityReferenceBase
Base class for entity reference formatters with field details.

Namespace

Drupal\blazy\Plugin\Field\FieldFormatter

Code

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

  // Title can be plain text, or link field.
  if (!empty($settings['title']) && isset($entity->{$settings['title']})) {
    if ($title = $this
      ->blazyEntity()
      ->getFieldTextOrLink($entity, $settings['title'], $settings)) {
      $element['caption']['title'] = $title;
    }
  }

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

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