View source
<?php
namespace Drupal\video_embed_wysiwyg\Plugin\Filter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\video_embed_field\Plugin\Field\FieldFormatter\Video;
use Drupal\video_embed_field\ProviderManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Render\RendererInterface;
class VideoEmbedWysiwyg extends FilterBase implements ContainerFactoryPluginInterface {
protected $providerManager;
protected $renderer;
protected $currentUser;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ProviderManagerInterface $provider_manager, RendererInterface $renderer, AccountProxyInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->providerManager = $provider_manager;
$this->renderer = $renderer;
$this->currentUser = $current_user;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('video_embed_field.provider_manager'), $container
->get('renderer'), $container
->get('current_user'));
}
public function process($text, $langcode) {
$response = new FilterProcessResult($text);
foreach ($this
->getValidMatches($text) as $source_text => $embed_data) {
if (!($provider = $this->providerManager
->loadProviderFromInput($embed_data['video_url']))) {
continue;
}
$autoplay = $this->currentUser
->hasPermission('never autoplay videos') ? FALSE : $embed_data['settings']['autoplay'];
$embed_code = $provider
->renderEmbedCode($embed_data['settings']['width'], $embed_data['settings']['height'], $autoplay);
$embed_code = [
'#type' => 'container',
'#attributes' => [
'class' => [
Html::cleanCssIdentifier(sprintf('video-embed-field-provider-%s', $provider
->getPluginId())),
],
],
'children' => $embed_code,
];
if ($embed_data['settings']['responsive']) {
$embed_code['#attributes']['class'][] = 'video-embed-field-responsive-video';
}
$text = str_replace($source_text, $this->renderer
->render($embed_code), $text);
$response
->setAttachments([
'library' => [
'video_embed_field/responsive-video',
],
]);
$response
->setCacheContexts([
'user.permissions',
]);
}
$response
->setProcessedText($text);
return $response;
}
protected function getValidMatches($text) {
if (!preg_match_all('/(<p>)?(?<json>{(?=.*preview_thumbnail\\b)(?=.*settings\\b)(?=.*video_url\\b)(?=.*settings_summary)(.*)})(<\\/p>)?/', $text, $matches)) {
return [];
}
$valid_matches = [];
foreach ($matches['json'] as $delta => $match) {
$embed_data = json_decode($match, TRUE);
if (!$embed_data || !is_array($embed_data)) {
continue;
}
if ($this
->isValidSettings($embed_data['settings'])) {
$valid_matches[$matches[0][$delta]] = $embed_data;
}
}
return $valid_matches;
}
protected function isValidSettings($settings) {
foreach (Video::defaultSettings() as $setting => $default) {
if (!isset($settings[$setting])) {
return FALSE;
}
}
return TRUE;
}
}