You are here

public function ShurlyFilter::process in ShURLy 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/ShurlyFilter.php, line 19

Class

ShurlyFilter
Plugin annotation @Filter( id = "shurly_filter", title = @Translation("Shorten all outgoing URL's"), description = @Translation("All links starting with http or https will be replaced."), type = …

Namespace

Drupal\shurly\Plugin\Filter

Code

public function process($text, $langcode) {
  preg_match_all('/<a[^>]*href="(http[^"]*)"[^>]*>/i', $text, $links);
  if (!empty($links)) {
    $links = $links[1];
    foreach ($links as $key => $link) {
      $short_url = shurly_shorten($link);
      if ($short_url['success'] === TRUE) {
        $text = str_replace('"' . $link . '"', '"' . $short_url['shortUrl'] . '"', $text);
      }
    }
  }
  return new FilterProcessResult($text);
}