Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function process($text, $langcode)
 {
     $response = new FilterProcessResult($text);
     // Use a look ahead to match the capture groups in any order.
     if (preg_match_all('/<p>(?<json>{(?=.*preview_thumbnail\\b)(?=.*settings\\b)(?=.*video_url\\b)(?=.*settings_summary)(.*)})<\\/p>/', $text, $matches)) {
         foreach ($matches['json'] as $delta => $match) {
             // Ensure the JSON string is valid.
             $embed_data = json_decode($match, TRUE);
             if (!is_array($embed_data)) {
                 continue;
             }
             // If the URL can't matched to a provider or the settings are invalid,
             // ignore it.
             $provider = $this->providerManager->loadProviderFromInput($embed_data['video_url']);
             if (!$provider || !$this->validSettings($embed_data['settings'])) {
                 continue;
             }
             $embed_code = $provider->renderEmbedCode($embed_data['settings']['width'], $embed_data['settings']['height'], $embed_data['settings']['autoplay']);
             // Add the container to make the video responsive if it's been
             //configured as such. This usually is attached to field output in the
             // case of a formatter, but a custom container must be used where one is
             // not present.
             if ($embed_data['settings']['responsive']) {
                 $embed_code = ['#type' => 'container', '#attributes' => ['class' => ['video-embed-field-responsive-video']], 'children' => $embed_code];
             }
             // Replace the JSON settings with a video.
             $text = str_replace($matches[0][$delta], $this->renderer->renderRoot($embed_code), $text);
         }
     }
     // Add the required responsive video library and update the response text.
     $response->setProcessedText($text);
     $response->addAttachments(['library' => ['video_embed_field/responsive-video']]);
     return $response;
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items, $langcode)
 {
     $element = [];
     $settings = $this->getSettings();
     foreach ($items as $delta => $item) {
         $provider = $this->providerManager->loadProviderFromInput($item->value);
         $element[$delta] = $provider->renderEmbedCode($settings['width'], $settings['height'], $settings['autoplay']);
     }
     return $element;
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items, $langcode)
 {
     $element = [];
     foreach ($items as $delta => $item) {
         $provider = $this->providerManager->loadProviderFromInput($item->value);
         $autoplay = $this->currentUser->hasPermission('never autoplay videos') ? FALSE : $this->getSetting('autoplay');
         $element[$delta] = $provider->renderEmbedCode($this->getSetting('width'), $this->getSetting('height'), $autoplay);
         $element[$delta]['#cache']['contexts'][] = 'user.permissions';
         // For responsive videos, wrap each field item in it's own container.
         if ($this->getSetting('responsive')) {
             $element[$delta] = ['#type' => 'container', '#attached' => ['library' => ['video_embed_field/responsive-video']], '#attributes' => ['class' => ['video-embed-field-responsive-video']], 'children' => $element[$delta]];
         }
     }
     return $element;
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items, $langcode)
 {
     $element = [];
     foreach ($items as $delta => $item) {
         $provider = $this->providerManager->loadProviderFromInput($item->value);
         $url = FALSE;
         if ($this->getSetting('link_image_to') == static::LINK_CONTENT) {
             $url = $items->getEntity()->urlInfo();
         } elseif ($this->getSetting('link_image_to') == static::LINK_PROVIDER) {
             $url = Url::fromUri($item->value);
         }
         $element[$delta] = $provider->renderThumbnail($this->getSetting('image_style'), $url);
     }
     return $element;
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items, $langcode) {
   $element = [];
   foreach ($items as $delta => $item) {
     $provider = $this->providerManager->loadProviderFromInput($item->value);
     $autoplay = $this->currentUser->hasPermission('never autoplay videos') ? FALSE : $this->getSetting('autoplay');
     $element[$delta] = $provider->renderEmbedCode($this->getSetting('width'), $this->getSetting('height'), $autoplay);
     $element[$delta]['#cache']['contexts'][] = 'user.permissions';
   }
   // Attach a library and attributes to the field renderable array in the case
   // of responsive videos.
   if ($this->getSetting('responsive')) {
     $element['#attached']['library'][] = 'video_embed_field/responsive-video';
     $element['#attributes']['class'][] = 'video-embed-field-responsive-video';
   }
   return $element;
 }
 /**
  * Load a video provider given a media entity.
  *
  * @param \Drupal\media_entity\MediaInterface $media
  *   The media entity.
  *
  * @return \Drupal\video_embed_field\ProviderPluginInterface
  *   The provider plugin.
  */
 protected function loadProvider(MediaInterface $media)
 {
     $video_url = $this->getVideoUrl($media);
     return !empty($video_url) ? $this->providerManager->loadProviderFromInput($video_url) : FALSE;
 }