You are here

public function TwigExtension::drupalUrl in Twig Tweak 8.2

Same name and namespace in other branches
  1. 8 src/TwigExtension.php \Drupal\twig_tweak\TwigExtension::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 TwigExtension::drupalUrl()
TwigExtension::drupalLink in src/TwigExtension.php
Generates a link from an internal path.

File

src/TwigExtension.php, line 915

Class

TwigExtension
Twig extension with some useful functions and filters.

Namespace

Drupal\twig_tweak

Code

public function drupalUrl($user_input, array $options = [], $check_access = FALSE) {
  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);
  if (!$check_access || $url
    ->access()) {
    return $url;
  }
}