You are here

public function BlazyEntityMediaBase::getCaption in Blazy 8.2

Builds captions with possible multi-value fields.

2 calls to BlazyEntityMediaBase::getCaption()
BlazyEntityMediaBase::buildElement in src/Dejavu/BlazyEntityMediaBase.php
Returns item contents.
BlazyEntityReferenceBase::getCaption in src/Dejavu/BlazyEntityReferenceBase.php
Builds slide captions with possible multi-value fields.
1 method overrides BlazyEntityMediaBase::getCaption()
BlazyEntityReferenceBase::getCaption in src/Dejavu/BlazyEntityReferenceBase.php
Builds slide captions with possible multi-value fields.

File

src/Dejavu/BlazyEntityMediaBase.php, line 131

Class

BlazyEntityMediaBase
Base class for Media 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'];

  // The caption fields common to all entity formatters, if so configured.
  if (!empty($settings['caption'])) {
    $caption_items = $weights = [];
    foreach ($settings['caption'] as $name => $field_caption) {

      /** @var Drupal\image\Plugin\Field\FieldType\ImageItem $item */
      if (isset($element['item']) && ($item = $element['item'])) {

        // Provides basic captions based on image attributes (Alt, Title).
        foreach ([
          'title',
          'alt',
        ] as $key => $attribute) {
          if ($name == $attribute && ($caption = trim($item
            ->get($attribute)
            ->getString()))) {
            $markup = Xss::filter($caption, BlazyDefault::TAGS);
            $caption_items[$name] = [
              '#markup' => $markup,
            ];
            $weights[] = $key;
          }
        }
      }
      if ($caption = $this
        ->blazyEntity()
        ->getFieldRenderable($entity, $field_caption, $view_mode)) {
        if (isset($caption['#weight'])) {
          $weights[] = $caption['#weight'];
        }
        $caption_items[$name] = $caption;
      }
    }
    if ($caption_items) {
      if ($weights) {
        array_multisort($weights, SORT_ASC, $caption_items);
      }

      // Differenciate Blazy from Slick, GridStack, etc. to avoid collisions.
      if ($settings['namespace'] == 'blazy') {
        $element['captions'] = $caption_items;
      }
      else {
        $element['caption']['data'] = $caption_items;
      }
    }
  }
}