Browse Source

Issue #2906117 by apsylone: Added drupal_url() function.

8.x-1.x
Chi 7 years ago
parent
commit
eb51aea30c
  1. 22
      src/TwigExtension.php
  2. 6
      tests/src/Functional/TwigTweakTest.php
  3. 1
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

22
src/TwigExtension.php

@ -5,6 +5,7 @@ namespace Drupal\twig_tweak;
use Drupal\Core\Block\TitleBlockPluginInterface; use Drupal\Core\Block\TitleBlockPluginInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Site\Settings; use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
use Drupal\image\Entity\ImageStyle; use Drupal\image\Entity\ImageStyle;
use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Cmf\Component\Routing\RouteObjectInterface;
@ -36,6 +37,7 @@ class TwigExtension extends \Twig_Extension {
// suitable for Twig template. // suitable for Twig template.
new \Twig_SimpleFunction('drupal_set_message', [$this, 'drupalSetMessage']), new \Twig_SimpleFunction('drupal_set_message', [$this, 'drupalSetMessage']),
new \Twig_SimpleFunction('drupal_title', [$this, 'drupalTitle']), 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; 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. * Replaces all tokens in a given string with appropriate values.
* *

6
tests/src/Functional/TwigTweakTest.php

@ -2,6 +2,7 @@
namespace Drupal\Tests\twig_tweak\Functional; namespace Drupal\Tests\twig_tweak\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase; use Drupal\Tests\BrowserTestBase;
/** /**
@ -137,6 +138,11 @@ class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "tt-title" and text() = "Beta"]'; $xpath = '//div[@class = "tt-title" and text() = "Beta"]';
$this->assertByXpath($xpath); $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. // Test token replacement.
$xpath = '//div[@class = "tt-token-replace" and text() = "Site name: Drupal"]'; $xpath = '//div[@class = "tt-token-replace" and text() = "Site name: Drupal"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);

1
tests/twig_tweak_test/templates/twig-tweak-test.html.twig

@ -34,6 +34,7 @@
<div class="tt-config">{{ drupal_config('user.settings', 'anonymous') }}</div> <div class="tt-config">{{ drupal_config('user.settings', 'anonymous') }}</div>
<div class="tt-status-message">{{ drupal_set_message('Hi!', 'warning') }}</div> <div class="tt-status-message">{{ drupal_set_message('Hi!', 'warning') }}</div>
<div class="tt-title">{{ drupal_title() }}</div> <div class="tt-title">{{ drupal_title() }}</div>
<div class="tt-url">{{ drupal_url('node/1', {absolute: true}) }}</div>
<div class="tt-token-replace">{{ 'Site name: [site:name]' | token_replace }}</div> <div class="tt-token-replace">{{ 'Site name: [site:name]' | token_replace }}</div>
<div class="tt-preg-replace">{{ 'foo' | preg_replace('(foo)', '$1-bar') }}</div> <div class="tt-preg-replace">{{ 'foo' | preg_replace('(foo)', '$1-bar') }}</div>
<div class="tt-image-style">{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}</div> <div class="tt-image-style">{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}</div>

Loading…
Cancel
Save