You are here

public function UrlShortener::process in Shorten URLs 8

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

{@inheritdocs}

Overrides FilterInterface::process

File

modules/shortener/src/Plugin/Filter/UrlShortener.php, line 59
Contains \Drupal\shortener\Plugin\Filter\UrlShortener.

Class

UrlShortener
Provides a filter to limit allowed HTML tags.

Namespace

Drupal\shortener\Plugin\Filter

Code

public function process($text, $langcode) {
  $length = $this->settings['shortener_url_length'];

  // Pass length to regexp callback.
  _filter_url_trim('', $length);

  // Pass behavior to regexp callback.
  $this
    ->_shortener_url_behavior(NULL, FALSE, $this->settings['shortener_url_behavior'], $length);
  $text = ' ' . $text . ' ';

  // Match absolute URLs.
  $text = preg_replace_callback("`(<p>|<li>|<br\\s*/?>|[ \n\r\t\\(])((http://|https://)([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))([.,?!]*?)(?=(</p>|</li>|<br\\s*/?>|[ \n\r\t\\)]))`i", array(
    get_class($this),
    '_shortener_url_behavior',
  ), $text);

  // Match www domains/addresses.
  $text = preg_replace_callback("`(<p>|<li>|[ \n\r\t\\(])(www\\.[a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+~#\\&=/;-])([.,?!]*?)(?=(</p>|</li>|<br\\s*/?>|[ \n\r\t\\)]))`i", array(
    get_class($this),
    '_shortener_url_parse_partial_links',
  ), $text);
  $text = substr($text, 1, -1);

  // return new FilterProcessResult($text);
  $result = new FilterProcessResult($text);
  $result
    ->setAttachments(array(
    'library' => array(
      'shortener/shortener',
    ),
  ));
  return $result;
}