You are here

public static function UrlValidator::rootToRelative in Mini site 8

Convert root-level URL to relative URL with parent prefix support.

3 calls to UrlValidator::rootToRelative()
PageProcessor::processTagA in src/PageProcessor.php
Process <a> tag.
PageProcessor::processTagLink in src/PageProcessor.php
Process <link> tag.
UrlValidatorTest::testRootToRelative in tests/src/Kernel/UrlValidatorTest.php
Tests for rootToRelative().

File

src/UrlValidator.php, line 45

Class

UrlValidator
Class UrlValidator.

Namespace

Drupal\minisite

Code

public static function rootToRelative($root_url, $parent = NULL, $prefix = NULL) {
  if (self::urlIsExternal($root_url) || !self::urlIsRoot($root_url)) {
    return $root_url;
  }
  if (substr($root_url, 0, 2) == './') {
    $root_url = substr($root_url, 2);
  }
  elseif (substr($root_url, 0, 1) == '/') {
    $root_url = substr($root_url, 1);
  }
  $parts = [];
  if ($prefix) {
    $parts[] = $prefix;
  }
  if ($parent) {
    $parts[] = $parent;
  }
  $parts[] = $root_url;
  return implode('/', $parts);
}