You are here

private function SoEmbedFilter::embed in Simple oEmbed 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Filter/SoEmbedFilter.php \Drupal\soembed\Plugin\Filter\SoEmbedFilter::embed()

Turn URL into iframe via oEmbed.

Most code copied from Drupal\media\Plugin\Field\FieldFormatter::viewElements().

File

src/Plugin/Filter/SoEmbedFilter.php, line 151

Class

SoEmbedFilter
Provides a filter to embed media via oEmbed.

Namespace

Drupal\soembed\Plugin\Filter

Code

private function embed($match) {
  $value = $match[2];
  $max_width = $this->settings['soembed_maxwidth'];
  $max_height = 0;
  try {
    $resource_url = $this->urlResolver
      ->getResourceUrl($value, $max_width, $max_height);
    $resource = $this->resourceFetcher
      ->fetchResource($resource_url);
  } catch (ResourceException $exception) {
    $this->logger
      ->error("Could not retrieve the remote URL (@url).", [
      '@url' => $value,
    ]);
    return $match[0];
  }
  if ($resource
    ->getType() === Resource::TYPE_LINK) {
    $build = [
      '#title' => $resource
        ->getTitle(),
      '#type' => 'link',
      '#url' => Url::fromUri($value),
    ];
  }
  elseif ($resource
    ->getType() === Resource::TYPE_PHOTO) {
    $build = [
      '#theme' => 'image',
      '#uri' => $resource
        ->getUrl()
        ->toString(),
      '#width' => $max_width ?: $resource
        ->getWidth(),
      '#height' => $max_height ?: $resource
        ->getHeight(),
    ];
  }
  else {
    $url = Url::fromRoute('media.oembed_iframe', [], [
      'query' => [
        'url' => $value,
        'max_width' => $max_width,
        'max_height' => $max_height,
        'hash' => $this->iFrameUrlHelper
          ->getHash($value, $max_width, $max_height),
      ],
    ]);
    $domain = $this->config
      ->get('iframe_domain');
    if ($domain) {
      $url
        ->setOption('base_url', $domain);
    }

    // Render videos and rich content in an iframe for security reasons.
    // @see: https://oembed.com/#section3
    $build = [
      '#type' => 'html_tag',
      '#tag' => 'iframe',
      '#attributes' => [
        'src' => $url
          ->toString(),
        'frameborder' => 0,
        'scrolling' => FALSE,
        'allowtransparency' => TRUE,
        'width' => $max_width ?: $resource
          ->getWidth(),
        'height' => $max_height ?: $resource
          ->getHeight(),
        'class' => [
          'media-oembed-content',
        ],
      ],
      '#attached' => [
        'library' => [
          'media/oembed.formatter',
        ],
      ],
    ];

    // An empty title attribute will disable title inheritance, so only
    // add it if the resource has a title.
    $title = $resource
      ->getTitle();
    if ($title) {
      $build['#attributes']['title'] = $title;
    }
    CacheableMetadata::createFromObject($resource)
      ->addCacheTags($this->config
      ->getCacheTags())
      ->applyTo($build);
  }
  return $match[1] . $this->renderer
    ->render($build) . $match[3];
}