You are here

public function UrlShortener::process in Shorten URLs 8.2

Same name and namespace in other branches
  1. 8 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 58

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
    ->shortenerUrlBehavior(NULL, FALSE, $this->settings['shortener_url_behavior'], $length);
  $text = ' ' . $text . ' ';

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

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

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