View source
<?php
namespace Drupal\soembed\Plugin\Filter;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\media\IFrameUrlHelper;
use Drupal\media\OEmbed\Resource;
use Drupal\media\OEmbed\ResourceException;
use Drupal\media\OEmbed\ResourceFetcherInterface;
use Drupal\media\OEmbed\UrlResolverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SoEmbedFilter extends FilterBase implements ContainerFactoryPluginInterface {
protected $config;
protected $logger;
protected $resourceFetcher;
protected $urlResolver;
protected $iFrameUrlHelper;
protected $renderer;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, ResourceFetcherInterface $resource_fetcher, UrlResolverInterface $url_resolver, IFrameUrlHelper $iframe_url_helper, RendererInterface $renderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->config = $config_factory
->get('media.settings');
$this->logger = $logger_factory
->get('media');
$this->resourceFetcher = $resource_fetcher;
$this->urlResolver = $url_resolver;
$this->iFrameUrlHelper = $iframe_url_helper;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('logger.factory'), $container
->get('media.oembed.resource_fetcher'), $container
->get('media.oembed.url_resolver'), $container
->get('media.oembed.iframe_url_helper'), $container
->get('renderer'));
}
public function process($text, $langcode) {
$lines = explode("\n", $text);
if (!empty($this->settings['soembed_replace_inline'])) {
$lines = preg_replace_callback('#([^"])(https?://[^\\s<]+)([^"])#', [
$this,
'embed',
], $lines);
}
else {
$lines = preg_replace_callback('#^(<p>)?(https?://\\S+?)(</p>)?$#', [
$this,
'embed',
], $lines);
}
$text = implode("\n", $lines);
return new FilterProcessResult($text);
}
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);
}
$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',
],
],
];
$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];
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form['soembed_maxwidth'] = [
'#type' => 'textfield',
'#title' => $this
->t('Maximum width of media embed'),
'#default_value' => $this->settings['soembed_maxwidth'],
'#description' => $this
->t('Leave to zero to use original values.'),
];
$form['soembed_replace_inline'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Replace in-line URLs'),
'#default_value' => $this->settings['soembed_replace_inline'],
'#description' => $this
->t('If this option is checked, the filter will recognize URLs even when they are not on their own line.'),
];
return $form;
}
}