Browse Source

Issue #2942287 by hanoii: Improve drupal_url with an optional access check and add drupal_link as well

merge-requests/4/head
hanoii 7 years ago committed by Chi
parent
commit
16b0cc1923
  1. 37
      src/TwigExtension.php
  2. 11
      tests/src/Functional/TwigTweakTest.php
  3. 2
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

37
src/TwigExtension.php

@ -4,6 +4,7 @@ namespace Drupal\twig_tweak;
use Drupal\Core\Block\TitleBlockPluginInterface; use Drupal\Core\Block\TitleBlockPluginInterface;
use Drupal\Core\Site\Settings; use Drupal\Core\Site\Settings;
use Drupal\Core\Link;
use Drupal\Core\Url; 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;
@ -37,6 +38,7 @@ class TwigExtension extends \Twig_Extension {
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']), new \Twig_SimpleFunction('drupal_url', [$this, 'drupalUrl']),
new \Twig_SimpleFunction('drupal_link', [$this, 'drupalLink']),
]; ];
} }
@ -352,23 +354,52 @@ class TwigExtension extends \Twig_Extension {
} }
/** /**
* Generates a URL from internal path. * Generates a URL from an internal path.
* *
* @param string $user_input * @param string $user_input
* User input for a link or path. * User input for a link or path.
* @param array $options * @param array $options
* (optional) An array of options. * (optional) An array of options.
* @param bool $check_access
* (Optional) Indicates that access check is required.
* *
* @return \Drupal\Core\Url * @return \Drupal\Core\Url
* A new Url object based on user input. * A new Url object based on user input.
* *
* @see \Drupal\Core\Url::fromUserInput() * @see \Drupal\Core\Url::fromUserInput()
*/ */
public function drupalUrl($user_input, array $options = []) { public function drupalUrl($user_input, array $options = [], $check_access = FALSE) {
if (!in_array($user_input[0], ['/', '#', '?'])) { if (!in_array($user_input[0], ['/', '#', '?'])) {
$user_input = '/' . $user_input; $user_input = '/' . $user_input;
} }
return Url::fromUserInput($user_input, $options); $url = Url::fromUserInput($user_input, $options);
if (!$check_access || $url->access()) {
return $url;
}
}
/**
* Generates a link from an internal path.
*
* @param string $text
* The text to be used for the link.
* @param string $user_input
* User input for a link or path.
* @param array $options
* (optional) An array of options.
* @param bool $check_access
* (Optional) Indicates that access check is required.
*
* @return \Drupal\Core\Link
* A new Link object.
*
* @see \Drupal\Core\Link::fromTextAndUrl()
*/
public function drupalLink($text, $user_input, array $options = [], $check_access = FALSE) {
$url = $this->drupalUrl($user_input, $options, $check_access);
if ($url) {
return Link::fromTextAndUrl($text, $url);
}
} }
/** /**

11
tests/src/Functional/TwigTweakTest.php

@ -2,6 +2,7 @@
namespace Drupal\Tests\twig_tweak\Functional; namespace Drupal\Tests\twig_tweak\Functional;
use Drupal\Core\Link;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase; use Drupal\Tests\BrowserTestBase;
@ -147,6 +148,16 @@ class TwigTweakTest extends BrowserTestBase {
$xpath = sprintf('//div[@class = "tt-url" and text() = "%s"]', $url); $xpath = sprintf('//div[@class = "tt-url" and text() = "%s"]', $url);
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test link.
$url = Url::fromUserInput('/node/1/edit', ['absolute' => TRUE]);
$link = Link::fromTextAndUrl('Edit', $url)->toString();
$xpath = '//div[@class = "tt-link"]';
$this->assertEquals($link, trim($this->xpath($xpath)[0]->getHtml()));
// Test protected link.
$xpath = '//div[@class = "tt-link-access"]';
$this->assertEquals('', trim($this->xpath($xpath)[0]->getHtml()));
// 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);

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

@ -36,6 +36,8 @@
<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-url">{{ drupal_url('node/1', {absolute: true}) }}</div>
<div class="tt-link">{{ drupal_link('Edit', 'node/1/edit', {absolute: true}) }}</div>
<div class="tt-link-access">{{ drupal_link('Edit', 'node/1/edit', {absolute: true}, 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)/i', '$1-bar') }}</div> <div class="tt-preg-replace">{{ 'FOO' | preg_replace('/(foo)/i', '$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