Browse Source

Added drupal_breadcrumb() function.

merge-requests/4/head
Chi 7 years ago
parent
commit
6d5069ef06
  1. 10
      src/TwigExtension.php
  2. 4
      tests/src/Functional/TwigTweakTest.php
  3. 1
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

10
src/TwigExtension.php

@ -46,6 +46,7 @@ class TwigExtension extends \Twig_Extension {
new \Twig_SimpleFunction('drupal_url', [$this, 'drupalUrl']),
new \Twig_SimpleFunction('drupal_link', [$this, 'drupalLink']),
new \Twig_SimpleFunction('drupal_messages', [$this, 'drupalMessages']),
new \Twig_SimpleFunction('drupal_breadcrumb', [$this, 'drupalBreadcrumb']),
];
}
@ -515,6 +516,15 @@ class TwigExtension extends \Twig_Extension {
return ['#type' => 'status_messages'];
}
/**
* Builds the breadcrumb.
*/
public function drupalBreadcrumb() {
return \Drupal::service('breadcrumb')
->build(\Drupal::routeMatch())
->toRenderable();
}
/**
* Replaces all tokens in a given string with appropriate values.
*

4
tests/src/Functional/TwigTweakTest.php

@ -199,6 +199,10 @@ class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "tt-messages"]/div[contains(@class, "messages--status") and contains(., "Hello world!")]';
$this->assertByXpath($xpath);
// Test breadcrumb.
$xpath = '//div[@class = "tt-breadcrumb"]/nav[@class = "breadcrumb"]/ol/li/a[text() = "Home"]';
$this->assertByXpath($xpath);
// Test protected link.
$xpath = '//div[@class = "tt-link-access"]';
self::assertEquals('', trim($this->xpath($xpath)[0]->getHtml()));

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

@ -44,6 +44,7 @@
<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-messages">{{ drupal_messages() }}</div>
<div class="tt-breadcrumb">{{ drupal_breadcrumb() }}</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-preg-replace">{{ 'FOO' | preg_replace('/(foo)/i', '$1-bar') }}</div>

Loading…
Cancel
Save