View source
<?php
namespace Drupal\blazy;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Url;
use Drupal\Core\Image\ImageFactory;
use Drupal\file\Entity\File;
use Drupal\media\IFrameUrlHelper;
use Drupal\media\OEmbed\ResourceFetcherInterface;
use Drupal\media\OEmbed\UrlResolverInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BlazyOEmbed implements BlazyOEmbedInterface {
protected $urlResolver;
protected $resourceFetcher;
protected $iframeUrlHelper;
protected $blazyManager;
protected $resource;
protected $request;
protected $imageFactory;
public function __construct(RequestStack $request, ResourceFetcherInterface $resource_fetcher, UrlResolverInterface $url_resolver, IFrameUrlHelper $iframe_url_helper, ImageFactory $image_factory, BlazyManagerInterface $blazy_manager) {
$this->request = $request;
$this->resourceFetcher = $resource_fetcher;
$this->urlResolver = $url_resolver;
$this->iframeUrlHelper = $iframe_url_helper;
$this->imageFactory = $image_factory;
$this->blazyManager = $blazy_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('request_stack'), $container
->get('media.oembed.resource_fetcher'), $container
->get('media.oembed.url_resolver'), $container
->get('media.oembed.iframe_url_helper'), $container
->get('image.factory'), $container
->get('blazy.manager'));
}
public function getResourceFetcher() {
return $this->resourceFetcher;
}
public function getUrlResolver() {
return $this->urlResolver;
}
public function getIframeUrlHelper() {
return $this->iframeUrlHelper;
}
public function imageFactory() {
return $this->imageFactory;
}
public function blazyManager() {
return $this->blazyManager;
}
public function getResource($input_url) {
if (!isset($this->resource[hash('md2', $input_url)])) {
$resource_url = $this->urlResolver
->getResourceUrl($input_url, 0, 0);
$this->resource[hash('md2', $input_url)] = $this->resourceFetcher
->fetchResource($resource_url);
}
return $this->resource[hash('md2', $input_url)];
}
public function build(array &$settings = []) {
if (empty($settings['_input_url'])) {
$this
->checkInputUrl($settings);
}
$url = Url::fromRoute('media.oembed_iframe', [], [
'query' => [
'url' => $settings['input_url'],
'max_width' => 0,
'max_height' => 0,
'hash' => $this->iframeUrlHelper
->getHash($settings['input_url'], 0, 0),
'blazy' => 1,
'autoplay' => empty($settings['media_switch']) ? 0 : 1,
],
]);
if (!empty($settings['iframe_domain'])) {
$url
->setOption('base_url', $settings['iframe_domain']);
}
$settings['embed_url'] = $url
->toString();
if (isset($settings['media_source'])) {
$settings['type'] = $settings['media_source'] == 'oembed:video' ? 'video' : $settings['media_source'];
$settings['type'] = $settings['media_source'] == 'video_embed_field' ? 'video' : $settings['type'];
}
}
public function checkInputUrl(array &$settings = []) {
$settings['input_url'] = UrlHelper::stripDangerousProtocols(trim($settings['input_url']));
if (strpos($settings['input_url'], 'youtube.com/embed') !== FALSE) {
$search = '/youtube\\.com\\/embed\\/([a-zA-Z0-9]+)/smi';
$replace = "youtube.com/watch?v=\$1";
$settings['input_url'] = preg_replace($search, $replace, $settings['input_url']);
}
$settings['_input_url'] = TRUE;
}
public function getAutoPlayUrl($url = '') {
$data = [];
if (!empty($url)) {
$data['oembed_url'] = $url;
if (strpos($url, 'autoplay') === FALSE || strpos($url, 'autoplay=0') !== FALSE) {
$data['autoplay_url'] = strpos($url, '?') === FALSE ? $url . '?autoplay=1' : $url . '&autoplay=1';
}
}
return $data;
}
public function getMediaItem(array &$data, $media) {
if ($media
->getEntityTypeId() != 'media') {
return;
}
BlazyMedia::mediaItem($data, $media);
$settings = $data['settings'];
switch ($settings['media_source']) {
case 'oembed':
case 'oembed:video':
case 'video_embed_field':
if ($input_url = $media
->getSource()
->getSourceFieldValue($media)) {
$settings['input_url'] = $input_url;
$this
->build($settings);
}
break;
case 'image':
$settings['type'] = 'image';
break;
default:
break;
}
if (empty($settings['type']) && ($build = BlazyMedia::build($media, $settings))) {
$data['content'][] = $build;
}
$data['settings'] = $settings;
}
public function getExternalImageItem(array &$settings) {
if (($resource = $this
->getResource($settings['input_url'])) && $resource
->getThumbnailUrl()) {
$settings['uri'] = $settings['image_url'] = $resource
->getThumbnailUrl()
->getUri();
$settings['type'] = $resource
->getType();
if (empty($settings['width'])) {
$settings['width'] = $resource
->getThumbnailWidth() ?: $resource
->getWidth();
$settings['height'] = $resource
->getThumbnailHeight() ?: $resource
->getHeight();
}
return Blazy::image($settings);
}
return NULL;
}
public function getImageItem($file) {
$data = [];
$entity = $file;
if (isset($file->entity) && !isset($file->alt)) {
$entity = $file->entity;
}
if ($entity instanceof File) {
if ($image = $this->imageFactory
->get($entity
->getFileUri())) {
BlazyMedia::fakeImageItem($data, $entity, $image);
}
}
return $data;
}
public function preprocessMediaOembedIframe(array &$variables) {
if (empty($variables['media'])) {
return;
}
try {
$request = $this->request
->getCurrentRequest();
$is_blazy = $request->query
->getInt('blazy', NULL);
$is_autoplay = $request->query
->getInt('autoplay', NULL);
$url = $request->query
->get('url');
if ($url && $is_blazy == 1) {
$dom = Html::load($variables['media']);
$iframe = $dom
->getElementsByTagName('iframe');
if ($iframe->length > 0) {
$embed_url = $iframe
->item(0)
->getAttribute('src');
$settings = $this
->getAutoPlayUrl($embed_url);
if ($is_autoplay == 1 && !empty($settings['autoplay_url'])) {
$iframe
->item(0)
->setAttribute('src', $settings['autoplay_url']);
}
$iframe
->item(0)
->setAttribute('height', '100%');
$iframe
->item(0)
->setAttribute('width', '100%');
$dom
->getElementsByTagName('body')
->item(0)
->setAttribute('class', 'is-b-oembed');
$variables['media'] = $dom
->saveHTML();
}
}
} catch (\Exception $ignore) {
}
}
}