示例#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;
 }
示例#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;
 }
示例#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;
 }
示例#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;
 }
示例#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;
 }
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items, $langcode)
 {
     // load widget settings
     $field_definition = $this->fieldDefinition;
     $entity_form_display = entity_get_form_display($field_definition->getTargetEntityTypeId(), $field_definition->getTargetBundle(), 'default');
     $widget = $entity_form_display->getRenderer($field_definition->getName());
     $widget_settings = $widget->getSettings();
     $element = [];
     foreach ($items as $delta => $item) {
         $file = File::load($item->target_id);
         $metadata = isset($item->data) ? unserialize($item->data) : array();
         $scheme = file_uri_scheme($file->getFileUri());
         $provider = $this->providerManager->loadProviderFromStream($scheme, $file, $metadata, $widget_settings);
         $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(file_create_url($file->getFileUri()));
         }
         $element[$delta] = $provider->renderThumbnail($this->getSetting('image_style'), $url);
     }
     return $element;
 }
 /**
  * {@inheritdoc}
  */
 public function fieldSettingsForm(array $form, FormStateInterface $form_state)
 {
     $form = [];
     $form['allowed_providers'] = ['#title' => t('Allowed Providers'), '#description' => t('Restrict users from entering information from the following providers. If none are selected any video provider can be used.'), '#type' => 'checkboxes', '#default_value' => $this->getSetting('allowed_providers'), '#options' => $this->providerManager->getProvidersOptionList()];
     return $form;
 }
 /**
  * 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;
 }