Browse Source

Added drupal_form function.

8.x-1.x 8.x-1.6
Chi 8 years ago
parent
commit
ef666024d6
  1. 15
      src/TwigExtension.php
  2. 28
      tests/src/Functional/TwigTweakTest.php
  3. 1
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig
  4. 2
      tests/twig_tweak_test/twig_tweak_test.module
  5. 1
      twig_tweak.services.yml

15
src/TwigExtension.php

@ -6,6 +6,7 @@ use Drupal\Core\Block\TitleBlockPluginInterface;
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\TitleResolverInterface; use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface; use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Site\Settings; use Drupal\Core\Site\Settings;
@ -68,6 +69,13 @@ class TwigExtension extends \Twig_Extension {
*/ */
protected $titleResolver; protected $titleResolver;
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;
/** /**
* TwigExtension constructor. * TwigExtension constructor.
* *
@ -85,8 +93,10 @@ class TwigExtension extends \Twig_Extension {
* The request stack. * The request stack.
* @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
* The 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->entityTypeManager = $entity_type_manager;
$this->token = $token; $this->token = $token;
$this->configFactory = $config_factory; $this->configFactory = $config_factory;
@ -94,6 +104,7 @@ class TwigExtension extends \Twig_Extension {
$this->menuTree = $menu_tree; $this->menuTree = $menu_tree;
$this->requestStack = $request_stack; $this->requestStack = $request_stack;
$this->titleResolver = $title_resolver; $this->titleResolver = $title_resolver;
$this->formBuilder = $form_builder;
} }
/** /**
@ -294,7 +305,7 @@ class TwigExtension extends \Twig_Extension {
* A render array to represent the form. * A render array to represent the form.
*/ */
public function drupalForm($form_id) { public function drupalForm($form_id) {
return \Drupal::formBuilder()->getForm($form_id); return $this->formBuilder->getForm($form_id);
} }
/** /**

28
tests/src/Functional/TwigTweakTest.php

@ -5,7 +5,7 @@ namespace Drupal\Tests\twig_tweak\Functional;
use Drupal\Tests\BrowserTestBase; use Drupal\Tests\BrowserTestBase;
/** /**
* Tests twig_tweak twig extension. * A test for Twig extension.
* *
* @group twig_tweak * @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() { public function testOutput() {
$this->drupalGet('<front>'); $this->drupalGet('<front>');
@ -62,6 +62,18 @@ class TwigTweakTest extends BrowserTestBase {
$xpath .= '/div[@class = "view-content"]//ul[count(./li) = 1]/li'; $xpath .= '/div[@class = "view-content"]//ul[count(./li) = 1]/li';
$this->assertByXpath($xpath . '//a[contains(@href, "/node/1") and text() = "Alpha"]'); $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. // Test entity default view mode.
$xpath = '//div[@class = "tt-entity-default"]'; $xpath = '//div[@class = "tt-entity-default"]';
$xpath .= '/article[contains(@class, "node") and not(contains(@class, "node--view-mode-teaser"))]'; $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"]'; $xpath = '//div[@class = "tt-menu-depth"]/ul[@class = "menu"]/li[not(ul)]/a[text() = "Link 1"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test region. // Test form.
$xpath = '//div[@class = "tt-region"]'; $xpath = '//div[@class = "tt-form"]/form[@class="system-cron-settings"]/input[@type = "submit" and @value = "Run cron"]';
$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")]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test token. // Test token.

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

@ -28,6 +28,7 @@
<div class="tt-menu-default">{{ drupal_menu('twig-tweak-test') }}</div> <div class="tt-menu-default">{{ drupal_menu('twig-tweak-test') }}</div>
<div class="tt-menu-level">{{ drupal_menu('twig-tweak-test', 2) }}</div> <div class="tt-menu-level">{{ drupal_menu('twig-tweak-test', 2) }}</div>
<div class="tt-menu-depth">{{ drupal_menu('twig-tweak-test', 1, 1) }}</div> <div class="tt-menu-depth">{{ drupal_menu('twig-tweak-test', 1, 1) }}</div>
<div class="tt-form">{{ drupal_form('Drupal\\system\\Form\\CronForm') }}</div>
<div class="tt-token">{{ drupal_token('site:name') }}</div> <div class="tt-token">{{ drupal_token('site:name') }}</div>
<div class="tt-token-data">{{ drupal_token('node:title', {'node': node}) }}</div> <div class="tt-token-data">{{ drupal_token('node:title', {'node': node}) }}</div>
<div class="tt-config">{{ drupal_config('user.settings', 'anonymous') }}</div> <div class="tt-config">{{ drupal_config('user.settings', 'anonymous') }}</div>

2
tests/twig_tweak_test/twig_tweak_test.module

@ -23,5 +23,5 @@ function twig_tweak_test_theme() {
* Prepares variables for twig-tweak-test template. * Prepares variables for twig-tweak-test template.
*/ */
function template_preprocess_twig_tweak_test(&$vars) { function template_preprocess_twig_tweak_test(&$vars) {
$vars['node'] = $node = Drupal::routeMatch()->getParameter('node'); $vars['node'] = Drupal::routeMatch()->getParameter('node');
} }

1
twig_tweak.services.yml

@ -9,5 +9,6 @@ services:
- '@menu.link_tree' - '@menu.link_tree'
- '@request_stack' - '@request_stack'
- '@title_resolver' - '@title_resolver'
- '@form_builder'
tags: tags:
- { name: twig.extension } - { name: twig.extension }

Loading…
Cancel
Save