You are here

public function OEmbedFormatter::viewElements in Cookie Content Blocker 8

Throws

\Exception

Overrides OEmbedFormatter::viewElements

File

modules/cookie_content_blocker_media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php, line 111

Class

OEmbedFormatter
Plugin implementation of the 'oembed' formatter.

Namespace

Drupal\cookie_content_blocker_media\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) : array {
  $element = parent::viewElements($items, $langcode);
  $config = $this->configFactory
    ->get('cookie_content_blocker_media.settings');
  if (empty($element)) {
    return $element;
  }

  /** @var \Drupal\Core\Field\FieldItemInterface $item */
  foreach ($items as $delta => $item) {
    if (!isset($element[$delta])) {
      continue;
    }

    /** @var \Drupal\media\MediaInterface $entity */

    /** @var \Drupal\media\Plugin\media\Source\OEmbedInterface $source */
    $entity = $item
      ->getEntity();
    $source = $entity
      ->getSource();
    $provider = $source
      ->getMetadata($entity, 'provider_name');
    $settings = $config
      ->get("providers.{$provider}");
    if (empty($settings['blocked'])) {
      continue;
    }
    $original = $element[$delta];
    $element[$delta]['#pre_render'] = $element[$delta]['#pre_render'] ?? [];
    $element[$delta]['#pre_render'][] = 'cookie_content_blocker.element.processor:processElement';
    $element[$delta]['#cookie_content_blocker'] = [
      'blocked_message' => $settings['blocked_message']['value'],
      'original_content' => $original,
    ];
    if (empty($settings['show_preview'])) {
      continue;
    }
    $element[$delta]['#cookie_content_blocker']['preview'] = [
      '#theme' => 'image_style',
      '#style_name' => $settings['preview_style'] ?? 'blocked_media_teaser',
      '#uri' => $source
        ->getMetadata($entity, 'thumbnail_uri'),
    ];
  }
  $this->renderer
    ->addCacheableDependency($element, $config);
  return $element;
}