You are here

public function BynderFormatter::viewElements in Bynder 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/BynderFormatter.php \Drupal\bynder\Plugin\Field\FieldFormatter\BynderFormatter::viewElements()
  2. 8.2 src/Plugin/Field/FieldFormatter/BynderFormatter.php \Drupal\bynder\Plugin\Field\FieldFormatter\BynderFormatter::viewElements()
  3. 4.0.x src/Plugin/Field/FieldFormatter/BynderFormatter.php \Drupal\bynder\Plugin\Field\FieldFormatter\BynderFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/BynderFormatter.php, line 112

Class

BynderFormatter
Plugin implementation of the 'Bynder' formatter.

Namespace

Drupal\bynder\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $settings = $this
    ->getSettings();
  $element = [];
  $is_entityreference = $this->fieldDefinition
    ->getType() == 'entity_reference';
  foreach ($items as $delta => $item) {

    /** @var \Drupal\media\MediaInterface $media_entity */
    if ($media_entity = $is_entityreference ? $item->entity : $items
      ->getEntity()) {

      /** @var \Drupal\media\MediaSourceInterface $source_plugin */
      $source_plugin = $media_entity
        ->getSource();
      if ($source_plugin instanceof Bynder && ($thumbnails = $source_plugin
        ->getMetadata($media_entity, 'thumbnail_urls'))) {
        $element['#attached']['library'][] = 'bynder/formatter';
        $element[$delta]['bynder_wrapper'] = [
          '#type' => 'container',
          '#attributes' => [
            'class' => [
              'bynder-wrapper',
            ],
          ],
        ];
        $thumbnail_uri = isset($thumbnails[$settings['thumbnail']]) ? $thumbnails[$settings['thumbnail']] : $thumbnails['webimage'];
        $element[$delta]['bynder_wrapper']['image'] = [
          '#theme' => 'image',
          '#uri' => $thumbnail_uri,
          '#attributes' => [
            'class' => [
              'bynder-image',
            ],
          ],
        ];
        if ($settings['title_field'] && $media_entity
          ->hasField($settings['title_field']) && !$media_entity
          ->get($settings['title_field'])
          ->isEmpty()) {
          $element[$delta]['bynder_wrapper']['image']['#title'] = $media_entity
            ->get($settings['title_field'])->value;
        }
        if ($settings['alt_field'] && $media_entity
          ->hasField($settings['alt_field']) && !$media_entity
          ->get($settings['alt_field'])
          ->isEmpty()) {
          $element[$delta]['bynder_wrapper']['image']['#alt'] = $media_entity
            ->get($settings['alt_field'])->value;
        }
        $this->renderer
          ->addCacheableDependency($element[$delta]['bynder_wrapper']['image'], $item);
        $element[$delta]['bynder_wrapper']['usage_image'] = [
          '#theme' => 'image',
          '#uri' => \Drupal::moduleHandler()
            ->getModule('bynder')
            ->getPath() . '/images/icons/bynder-logo.png',
          '#alt' => 'usage-image',
          // @todo Information is not available yet. Fix when API supports it.
          '#title' => $this
            ->t('Usage info is not available yet. Usage restriction level: @restriction', [
            '@restriction' => get_media_restriction($source_plugin
              ->getMetadata($media_entity, 'propertyOptions')),
          ]),
          '#attributes' => [
            'class' => [
              'usage-image',
            ],
          ],
          '#access' => AccessResult::allowedIfHasPermission($this->currentUser, 'view bynder media usage'),
        ];
        $this->renderer
          ->addCacheableDependency($element[$delta]['bynder_wrapper']['usage_image'], $this->configFactory
          ->get('bynder.settings'));
      }
    }
  }
  return $element;
}