You are here

public static function TwigTweakExtension::drupalLink in Twig Tweak 3.x

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

Generates a link from an internal path.

Parameters

string|\Twig\Markup $text: The text to be used for the link.

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\Link|null A new Link object or null of the URL is not accessible.

See also

\Drupal\Core\Link::fromTextAndUrl()

File

src/TwigTweakExtension.php, line 372

Class

TwigTweakExtension
Twig extension with some useful functions and filters.

Namespace

Drupal\twig_tweak

Code

public static function drupalLink($text, string $user_input, array $options = [], bool $check_access = FALSE) : ?Link {
  $url = self::drupalUrl($user_input, $options, $check_access);
  if ($url) {

    // The text has been processed by twig already, convert it to a safe
    // object for the render system.
    // @see \Drupal\Core\Template\TwigExtension::getLink()
    if ($text instanceof TwigMarkup) {
      $text = Markup::create($text);
    }
    return Link::fromTextAndUrl($text, $url);
  }
  return NULL;
}