You are here

public function VideoEmbedWysiwyg::process in Video Embed Field 8

Same name and namespace in other branches
  1. 8.2 modules/video_embed_wysiwyg/src/Plugin/Filter/VideoEmbedWysiwyg.php \Drupal\video_embed_wysiwyg\Plugin\Filter\VideoEmbedWysiwyg::process()

Performs the filter processing.

Parameters

string $text: The text string to be filtered.

string $langcode: The language code of the text to be filtered.

Return value

\Drupal\filter\FilterProcessResult The filtered text, wrapped in a FilterProcessResult object, and possibly with associated assets, cacheability metadata and placeholders.

Overrides FilterInterface::process

See also

\Drupal\filter\FilterProcessResult

File

modules/video_embed_wysiwyg/src/Plugin/Filter/VideoEmbedWysiwyg.php, line 81

Class

VideoEmbedWysiwyg
The filter to turn tokens inserted into the WYSIWYG into videos.

Namespace

Drupal\video_embed_wysiwyg\Plugin\Filter

Code

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,
    ];

    // 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['#attributes']['class'][] = 'video-embed-field-responsive-video';
    }

    // Replace the JSON settings with a video.
    $text = str_replace($source_text, $this->renderer
      ->render($embed_code), $text);

    // Add the required responsive video library only when at least one match
    // is present.
    $response
      ->setAttachments([
      'library' => [
        'video_embed_field/responsive-video',
      ],
    ]);
    $response
      ->setCacheContexts([
      'user.permissions',
    ]);
  }
  $response
    ->setProcessedText($text);
  return $response;
}