You are here

public function TwigExtension::drupalUrl in Twig Tweak 8

Same name and namespace in other branches
  1. 8.2 src/TwigExtension.php \Drupal\twig_tweak\TwigExtension::drupalUrl()

Generates a URL from internal path.

Parameters

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

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

Return value

\Drupal\Core\Url A new Url object based on user input.

See also

\Drupal\Core\Url::fromUserInput()

File

src/TwigExtension.php, line 386

Class

TwigExtension
Twig extension with some useful functions and filters.

Namespace

Drupal\twig_tweak

Code

public function drupalUrl($user_input, array $options = []) {
  if (!in_array($user_input[0], [
    '/',
    '#',
    '?',
  ])) {
    $user_input = '/' . $user_input;
  }
  return Url::fromUserInput($user_input, $options);
}