From ef666024d617519dd800e6604ee79033ebd6639c Mon Sep 17 00:00:00 2001 From: Chi Date: Sun, 19 Mar 2017 16:05:18 +0300 Subject: [PATCH] Added drupal_form function. --- src/TwigExtension.php | 15 ++++++++-- tests/src/Functional/TwigTweakTest.php | 28 +++++++++++-------- .../templates/twig-tweak-test.html.twig | 1 + tests/twig_tweak_test/twig_tweak_test.module | 2 +- twig_tweak.services.yml | 1 + 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/TwigExtension.php b/src/TwigExtension.php index c66a984..d445642 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -6,6 +6,7 @@ use Drupal\Core\Block\TitleBlockPluginInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Controller\TitleResolverInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Menu\MenuLinkTreeInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Site\Settings; @@ -68,6 +69,13 @@ class TwigExtension extends \Twig_Extension { */ protected $titleResolver; + /** + * The form builder. + * + * @var \Drupal\Core\Form\FormBuilderInterface + */ + protected $formBuilder; + /** * TwigExtension constructor. * @@ -85,8 +93,10 @@ class TwigExtension extends \Twig_Extension { * The request stack. * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver * The title resolver. + * @param \Drupal\Core\Form\FormBuilderInterface $form_builder + * The form builder. */ - public function __construct(EntityTypeManagerInterface $entity_type_manager, Token $token, ConfigFactoryInterface $config_factory, RouteMatchInterface $route_match, MenuLinkTreeInterface $menu_tree, RequestStack $request_stack, TitleResolverInterface $title_resolver) { + public function __construct(EntityTypeManagerInterface $entity_type_manager, Token $token, ConfigFactoryInterface $config_factory, RouteMatchInterface $route_match, MenuLinkTreeInterface $menu_tree, RequestStack $request_stack, TitleResolverInterface $title_resolver, FormBuilderInterface $form_builder) { $this->entityTypeManager = $entity_type_manager; $this->token = $token; $this->configFactory = $config_factory; @@ -94,6 +104,7 @@ class TwigExtension extends \Twig_Extension { $this->menuTree = $menu_tree; $this->requestStack = $request_stack; $this->titleResolver = $title_resolver; + $this->formBuilder = $form_builder; } /** @@ -294,7 +305,7 @@ class TwigExtension extends \Twig_Extension { * A render array to represent the form. */ public function drupalForm($form_id) { - return \Drupal::formBuilder()->getForm($form_id); + return $this->formBuilder->getForm($form_id); } /** diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index 6ddb989..4868a83 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/tests/src/Functional/TwigTweakTest.php @@ -5,7 +5,7 @@ namespace Drupal\Tests\twig_tweak\Functional; use Drupal\Tests\BrowserTestBase; /** - * Tests twig_tweak twig extension. + * A test for Twig extension. * * @group twig_tweak */ @@ -35,7 +35,7 @@ class TwigTweakTest extends BrowserTestBase { } /** - * Tests output produced by the twig extension. + * Tests output produced by the Twig extension. */ public function testOutput() { $this->drupalGet(''); @@ -62,6 +62,18 @@ class TwigTweakTest extends BrowserTestBase { $xpath .= '/div[@class = "view-content"]//ul[count(./li) = 1]/li'; $this->assertByXpath($xpath . '//a[contains(@href, "/node/1") and text() = "Alpha"]'); + // Test block. + $xpath = '//div[@class = "tt-block"]'; + $xpath .= '/div[@id="block-powered-by-drupal"]/span[contains(., "Powered by Drupal")]'; + $this->assertByXpath($xpath); + + // Test region. + $xpath = '//div[@class = "tt-region"]'; + $xpath .= '/div[contains(@class, "block-page-title-block") and h1[@class="page-title" and text() = "Log in"]]'; + $xpath .= '/following-sibling::div[@class="messages messages--warning" and contains(., "Hi!")]'; + $xpath .= '/following-sibling::div[contains(@class, "block-system-powered-by-block")]/span[. = "Powered by Drupal"]'; + $this->assertByXpath($xpath); + // Test entity default view mode. $xpath = '//div[@class = "tt-entity-default"]'; $xpath .= '/article[contains(@class, "node") and not(contains(@class, "node--view-mode-teaser"))]'; @@ -99,16 +111,8 @@ class TwigTweakTest extends BrowserTestBase { $xpath = '//div[@class = "tt-menu-depth"]/ul[@class = "menu"]/li[not(ul)]/a[text() = "Link 1"]'; $this->assertByXpath($xpath); - // Test region. - $xpath = '//div[@class = "tt-region"]'; - $xpath .= '/div[contains(@class, "block-page-title-block") and h1[@class="page-title" and text() = "Beta"]]'; - $xpath .= '/following-sibling::div[@class="messages messages--warning" and contains(., "Hi!")]'; - $xpath .= '/following-sibling::div[contains(@class, "block-system-powered-by-block")]/span[. = "Powered by Drupal"]'; - $this->assertByXpath($xpath); - - // Test block. - $xpath = '//div[@class = "tt-block"]'; - $xpath .= '/div[@id="block-powered-by-drupal"]/span[contains(., "Powered by Drupal")]'; + // Test form. + $xpath = '//div[@class = "tt-form"]/form[@class="system-cron-settings"]/input[@type = "submit" and @value = "Run cron"]'; $this->assertByXpath($xpath); // Test token. 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 650c6c0..0f019f0 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -28,6 +28,7 @@
{{ drupal_menu('twig-tweak-test') }}
{{ drupal_menu('twig-tweak-test', 2) }}
{{ drupal_menu('twig-tweak-test', 1, 1) }}
+
{{ drupal_form('Drupal\\system\\Form\\CronForm') }}
{{ drupal_token('site:name') }}
{{ drupal_token('node:title', {'node': node}) }}
{{ drupal_config('user.settings', 'anonymous') }}
diff --git a/tests/twig_tweak_test/twig_tweak_test.module b/tests/twig_tweak_test/twig_tweak_test.module index 69d5703..f7d00aa 100644 --- a/tests/twig_tweak_test/twig_tweak_test.module +++ b/tests/twig_tweak_test/twig_tweak_test.module @@ -23,5 +23,5 @@ function twig_tweak_test_theme() { * Prepares variables for twig-tweak-test template. */ function template_preprocess_twig_tweak_test(&$vars) { - $vars['node'] = $node = Drupal::routeMatch()->getParameter('node'); + $vars['node'] = Drupal::routeMatch()->getParameter('node'); } diff --git a/twig_tweak.services.yml b/twig_tweak.services.yml index d040ea0..bb1f860 100644 --- a/twig_tweak.services.yml +++ b/twig_tweak.services.yml @@ -9,5 +9,6 @@ services: - '@menu.link_tree' - '@request_stack' - '@title_resolver' + - '@form_builder' tags: - { name: twig.extension }