From eb51aea30c07fd04316bc0ede074f62111ef4b8f Mon Sep 17 00:00:00 2001 From: Chi Date: Sun, 10 Sep 2017 17:12:06 +0500 Subject: [PATCH] Issue #2906117 by apsylone: Added drupal_url() function. --- src/TwigExtension.php | 22 +++++++++++++++++++ tests/src/Functional/TwigTweakTest.php | 6 +++++ .../templates/twig-tweak-test.html.twig | 1 + 3 files changed, 29 insertions(+) diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 614b10d..9016ec4 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -5,6 +5,7 @@ namespace Drupal\twig_tweak; use Drupal\Core\Block\TitleBlockPluginInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Site\Settings; +use Drupal\Core\Url; use Drupal\image\Entity\ImageStyle; use Symfony\Cmf\Component\Routing\RouteObjectInterface; @@ -36,6 +37,7 @@ class TwigExtension extends \Twig_Extension { // suitable for Twig template. new \Twig_SimpleFunction('drupal_set_message', [$this, 'drupalSetMessage']), new \Twig_SimpleFunction('drupal_title', [$this, 'drupalTitle']), + new \Twig_SimpleFunction('drupal_url', [$this, 'drupalUrl']), ]; } @@ -335,6 +337,26 @@ class TwigExtension extends \Twig_Extension { return $build; } + /** + * Generates a URL from internal path. + * + * @param string $user_input + * User input for a link or path. + * @param array $options + * (optional) An array of options. + * + * @return \Drupal\Core\Url + * A new Url object based on user input. + * + * @see \Drupal\Core\Url::fromUserInput() + */ + public function drupalUrl($user_input, array $options = []) { + if (!in_array($user_input[0], ['/', '#', '?'])) { + $user_input = '/' . $user_input; + } + return Url::fromUserInput($user_input, $options); + } + /** * Replaces all tokens in a given string with appropriate values. * diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index 5cfa762..432a9d8 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/tests/src/Functional/TwigTweakTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\twig_tweak\Functional; +use Drupal\Core\Url; use Drupal\Tests\BrowserTestBase; /** @@ -137,6 +138,11 @@ class TwigTweakTest extends BrowserTestBase { $xpath = '//div[@class = "tt-title" and text() = "Beta"]'; $this->assertByXpath($xpath); + // Test URL. + $url = Url::fromUserInput('/node/1', ['absolute' => TRUE])->toString(); + $xpath = sprintf('//div[@class = "tt-url" and text() = "%s"]', $url); + $this->assertByXpath($xpath); + // Test token replacement. $xpath = '//div[@class = "tt-token-replace" and text() = "Site name: Drupal"]'; $this->assertByXpath($xpath); diff --git a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig index ed94972..59985e7 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -34,6 +34,7 @@
{{ drupal_config('user.settings', 'anonymous') }}
{{ drupal_set_message('Hi!', 'warning') }}
{{ drupal_title() }}
+
{{ drupal_url('node/1', {absolute: true}) }}
{{ 'Site name: [site:name]' | token_replace }}
{{ 'foo' | preg_replace('(foo)', '$1-bar') }}
{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}