From 6d5069ef063cbf363474d532c93e43da402d3ccc Mon Sep 17 00:00:00 2001 From: Chi Date: Sat, 14 Apr 2018 14:13:58 +0500 Subject: [PATCH] Added drupal_breadcrumb() function. --- src/TwigExtension.php | 10 ++++++++++ tests/src/Functional/TwigTweakTest.php | 4 ++++ .../templates/twig-tweak-test.html.twig | 1 + 3 files changed, 15 insertions(+) diff --git a/src/TwigExtension.php b/src/TwigExtension.php index fb80467..6c2e94e 100644 --- a/src/TwigExtension.php +++ b/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. * diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index 9cc03e7..54b812f 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/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())); 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 a071ed9..de03b78 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -44,6 +44,7 @@
{{ drupal_url('node/1', {absolute: true}) }}
{{ drupal_messages() }}
+
{{ drupal_breadcrumb() }}
{{ 'Site name: [site:name]' | token_replace }}
{{ 'FOO' | preg_replace('/(foo)/i', '$1-bar') }}