3 namespace Drupal\video_embed_wysiwyg\Plugin\Filter;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\Core\Session\AccountProxyInterface;
8 use Drupal\filter\FilterProcessResult;
9 use Drupal\filter\Plugin\FilterBase;
10 use Drupal\video_embed_field\Plugin\Field\FieldFormatter\Video;
11 use Drupal\video_embed_field\ProviderManagerInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Drupal\Core\Render\RendererInterface;
16 * The filter to turn tokens inserted into the WYSIWYG into videos.
19 * title = @Translation("Video Embed WYSIWYG"),
20 * id = "video_embed_wysiwyg",
21 * description = @Translation("Enables the use of video_embed_wysiwyg."),
22 * type = Drupal\filter\Plugin\FilterInterface::TYPE_MARKUP_LANGUAGE
25 class VideoEmbedWysiwyg extends FilterBase implements ContainerFactoryPluginInterface {
28 * The video provider manager.
30 * @var \Drupal\video_embed_field\ProviderManagerInterface
32 protected $providerManager;
37 * @var \Drupal\Core\Render\RendererInterface
44 * @var \Drupal\Core\Session\AccountProxyInterface
46 protected $currentUser;
49 * VideoEmbedWysiwyg constructor.
51 * @param array $configuration
52 * Plugin configuration.
53 * @param string $plugin_id
55 * @param mixed $plugin_definition
57 * @param \Drupal\video_embed_field\ProviderManagerInterface $provider_manager
58 * The video provider manager.
59 * @param \Drupal\Core\Render\RendererInterface $renderer
61 * @param \Drupal\Core\Session\AccountProxyInterface $current_user
64 public function __construct(array $configuration, $plugin_id, $plugin_definition, ProviderManagerInterface $provider_manager, RendererInterface $renderer, AccountProxyInterface $current_user) {
65 parent::__construct($configuration, $plugin_id, $plugin_definition);
66 $this->providerManager = $provider_manager;
67 $this->renderer = $renderer;
68 $this->currentUser = $current_user;
74 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
75 return new static($configuration, $plugin_id, $plugin_definition, $container->get('video_embed_field.provider_manager'), $container->get('renderer'), $container->get('current_user'));
81 public function process($text, $langcode) {
83 $response = new FilterProcessResult($text);
85 foreach ($this->getValidMatches($text) as $source_text => $embed_data) {
86 if (!$provider = $this->providerManager->loadProviderFromInput($embed_data['video_url'])) {
90 $autoplay = $this->currentUser->hasPermission('never autoplay videos') ? FALSE : $embed_data['settings']['autoplay'];
91 $embed_code = $provider->renderEmbedCode($embed_data['settings']['width'], $embed_data['settings']['height'], $autoplay);
94 '#type' => 'container',
96 'class' => [Html::cleanCssIdentifier(sprintf('video-embed-field-provider-%s', $provider->getPluginId()))],
98 'children' => $embed_code,
101 // Add the container to make the video responsive if it's been
102 // configured as such. This usually is attached to field output in the
103 // case of a formatter, but a custom container must be used where one is
105 if ($embed_data['settings']['responsive']) {
106 $embed_code['#attributes']['class'][] = 'video-embed-field-responsive-video';
109 // Replace the JSON settings with a video.
110 $text = str_replace($source_text, $this->renderer->render($embed_code), $text);
112 // Add the required responsive video library only when at least one match
114 $response->setAttachments(['library' => ['video_embed_field/responsive-video']]);
115 $response->setCacheContexts(['user.permissions']);
118 $response->setProcessedText($text);
123 * Get all valid matches in the WYSIWYG.
125 * @param string $text
126 * The text to check for WYSIWYG matches.
129 * An array of data from the text keyed by the text content.
131 protected function getValidMatches($text) {
132 // Use a look ahead to match the capture groups in any order.
133 if (!preg_match_all('/(<p>)?(?<json>{(?=.*preview_thumbnail\b)(?=.*settings\b)(?=.*video_url\b)(?=.*settings_summary)(.*)})(<\/p>)?/', $text, $matches)) {
137 foreach ($matches['json'] as $delta => $match) {
138 // Ensure the JSON string is valid.
139 $embed_data = json_decode($match, TRUE);
140 if (!$embed_data || !is_array($embed_data)) {
143 if ($this->isValidSettings($embed_data['settings'])) {
144 $valid_matches[$matches[0][$delta]] = $embed_data;
147 return $valid_matches;
151 * Check if the given settings are valid.
153 * @param array $settings
154 * Settings to validate.
157 * If the required settings are present.
159 protected function isValidSettings($settings) {
160 foreach (Video::defaultSettings() as $setting => $default) {
161 if (!isset($settings[$setting])) {