Browse Source

Issue #2978898 by hanoii: Add support for HTML markup on drupal_link

merge-requests/4/head 8.x-2.1
hanoii 6 years ago committed by Chi
parent
commit
6aae559040
  1. 7
      src/TwigExtension.php
  2. 8
      tests/src/Functional/TwigTweakTest.php
  3. 1
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

7
src/TwigExtension.php

@ -13,6 +13,7 @@ use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Link; use Drupal\Core\Link;
use Drupal\Core\Plugin\ContextAwarePluginInterface; use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Render\Element; use Drupal\Core\Render\Element;
use Drupal\Core\Render\Markup;
use Drupal\Core\Site\Settings; use Drupal\Core\Site\Settings;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\image\Entity\ImageStyle; use Drupal\image\Entity\ImageStyle;
@ -535,6 +536,12 @@ class TwigExtension extends \Twig_Extension {
public function drupalLink($text, $user_input, array $options = [], $check_access = FALSE) { public function drupalLink($text, $user_input, array $options = [], $check_access = FALSE) {
$url = $this->drupalUrl($user_input, $options, $check_access); $url = $this->drupalUrl($user_input, $options, $check_access);
if ($url) { 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 \Twig_Markup) {
$text = Markup::create($text);
}
return Link::fromTextAndUrl($text, $url); return Link::fromTextAndUrl($text, $url);
} }
} }

8
tests/src/Functional/TwigTweakTest.php

@ -6,6 +6,7 @@ use Drupal\Core\Link;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\file\Entity\File; use Drupal\file\Entity\File;
use Drupal\responsive_image\Entity\ResponsiveImageStyle; use Drupal\responsive_image\Entity\ResponsiveImageStyle;
use Drupal\Core\Render\Markup;
use Drupal\Tests\BrowserTestBase; use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role; use Drupal\user\Entity\Role;
@ -222,6 +223,13 @@ class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "tt-link"]'; $xpath = '//div[@class = "tt-link"]';
self::assertEquals($link, trim($this->xpath($xpath)[0]->getHtml())); self::assertEquals($link, trim($this->xpath($xpath)[0]->getHtml()));
// Test link with HTML.
$text = Markup::create('<b>Edit</b>');
$url = Url::fromUserInput('/node/1/edit', ['absolute' => TRUE]);
$link = Link::fromTextAndUrl($text, $url)->toString();
$xpath = '//div[@class = "tt-link-html"]';
self::assertEquals($link, trim($this->xpath($xpath)[0]->getHtml()));
// Test status messages. // Test status messages.
$xpath = '//div[@class = "tt-messages"]/div[contains(@class, "messages--status") and contains(., "Hello world!")]'; $xpath = '//div[@class = "tt-messages"]/div[contains(@class, "messages--status") and contains(., "Hello world!")]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);

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

@ -45,6 +45,7 @@
<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">{{ drupal_link('Edit', 'node/1/edit', {absolute: true}) }}</div>
<div class="tt-link-html">{% set link_text %}<b>Edit</b>{% endset %}{{ drupal_link(link_text, 'node/1/edit', {absolute: true}) }}</div>
<div class="tt-messages">{{ drupal_messages() }}</div> <div class="tt-messages">{{ drupal_messages() }}</div>
<div class="tt-breadcrumb">{{ drupal_breadcrumb() }}</div> <div class="tt-breadcrumb">{{ drupal_breadcrumb() }}</div>
<div class="tt-link-access">{{ drupal_link('Administration', 'admin', {absolute: true}, true) }}</div> <div class="tt-link-access">{{ drupal_link('Administration', 'admin', {absolute: true}, true) }}</div>

Loading…
Cancel
Save