You are here

public static function TwigTweakExtension::drupalUrl in Twig Tweak 3.1.x

Same name and namespace in other branches
  1. 3.x src/TwigTweakExtension.php \Drupal\twig_tweak\TwigTweakExtension::drupalUrl()

Generates a URL from an internal path.

Parameters

string $user_input: User input for a link or path.

array $options: (optional) An array of options.

bool $check_access: (optional) Indicates that access check is required.

Return value

\Drupal\Core\Url|null A new Url object or null if the URL is not accessible.

See also

\Drupal\Core\Url::fromUserInput()

1 call to TwigTweakExtension::drupalUrl()
TwigTweakExtension::drupalLink in src/TwigTweakExtension.php
Generates a link from an internal path.

File

src/TwigTweakExtension.php, line 341

Class

TwigTweakExtension
Twig extension with some useful functions and filters.

Namespace

Drupal\twig_tweak

Code

public static function drupalUrl(string $user_input, array $options = [], bool $check_access = FALSE) : ?Url {
  if (isset($options['langcode'])) {
    $language_manager = \Drupal::languageManager();
    if ($language = $language_manager
      ->getLanguage($options['langcode'])) {
      $options['language'] = $language;
    }
  }
  if (!in_array($user_input[0], [
    '/',
    '#',
    '?',
  ])) {
    $user_input = '/' . $user_input;
  }
  $url = Url::fromUserInput($user_input, $options);
  return !$check_access || $url
    ->access() ? $url : NULL;
}