UrlEmbedFilter.php in URL Embed 8
File
src/Plugin/Filter/UrlEmbedFilter.php
View source
<?php
namespace Drupal\url_embed\Plugin\Filter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\embed\DomHelperTrait;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\url_embed\UrlEmbedHelperTrait;
use Drupal\url_embed\UrlEmbedInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UrlEmbedFilter extends FilterBase implements ContainerFactoryPluginInterface {
use DomHelperTrait;
use UrlEmbedHelperTrait;
public function __construct(array $configuration, $plugin_id, $plugin_definition, UrlEmbedInterface $url_embed) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this
->setUrlEmbed($url_embed);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('url_embed'));
}
public function process($text, $langcode) {
$result = new FilterProcessResult($text);
if (strpos($text, 'data-embed-url') !== FALSE) {
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
foreach ($xpath
->query('//drupal-url[@data-embed-url]') as $node) {
$url = $node
->getAttribute('data-embed-url');
$url_output = '';
try {
if ($info = $this
->urlEmbed()
->getEmbed($url)) {
$url_output = $info
->getCode();
}
} catch (\Exception $e) {
watchdog_exception('url_embed', $e);
}
$this
->replaceNodeContent($node, $url_output);
}
$result
->setProcessedText(Html::serialize($dom));
}
return $result;
}
public function tips($long = FALSE) {
if ($long) {
return $this
->t('
<p>You can embed URLs. Additional properties can be added to the URL tag like data-caption and data-align if supported. Examples:</p>
<ul>
<li><code><drupal-url data-embed-url="https://www.youtube.com/watch?v=xxXXxxXxxxX" data-url-provider="YouTube" /></code></li>
</ul>');
}
else {
return $this
->t('You can embed URLs.');
}
}
}
Classes
Name |
Description |
UrlEmbedFilter |
Provides a filter to display embedded URLs based on data attributes. |