You are here

public function UrlEmbedFilter::process in URL Embed 8

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

src/Plugin/Filter/UrlEmbedFilter.php, line 65
Contains \Drupal\url_embed\Plugin\Filter\UrlEmbedFilter.

Class

UrlEmbedFilter
Provides a filter to display embedded URLs based on data attributes.

Namespace

Drupal\url_embed\Plugin\Filter

Code

public function process($text, $langcode) {
  $result = new FilterProcessResult($text);
  if (strpos($text, 'data-embed-url') !== FALSE) {
    $dom = Html::load($text);
    $xpath = new \DOMXPath($dom);
    foreach ($xpath
      ->query('//drupal-url[@data-embed-url]') as $node) {

      /** @var \DOMElement $node */
      $url = $node
        ->getAttribute('data-embed-url');
      $url_output = '';
      try {
        if ($info = $this
          ->urlEmbed()
          ->getEmbed($url)) {
          $url_output = $info
            ->getCode();
        }
      } catch (\Exception $e) {
        watchdog_exception('url_embed', $e);
      }
      $this
        ->replaceNodeContent($node, $url_output);
    }
    $result
      ->setProcessedText(Html::serialize($dom));
  }
  return $result;
}