Browse Source

Added drupal_entity_form() Twig function.

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

25
src/TwigExtension.php

@ -37,6 +37,7 @@ class TwigExtension extends \Twig_Extension {
new \Twig_SimpleFunction('drupal_block', [$this, 'drupalBlock']),
new \Twig_SimpleFunction('drupal_region', [$this, 'drupalRegion']),
new \Twig_SimpleFunction('drupal_entity', [$this, 'drupalEntity']),
new \Twig_SimpleFunction('drupal_entity_form', [$this, 'drupalEntityForm']),
new \Twig_SimpleFunction('drupal_field', [$this, 'drupalField']),
new \Twig_SimpleFunction('drupal_menu', [$this, 'drupalMenu']),
new \Twig_SimpleFunction('drupal_form', [$this, 'drupalForm']),
@ -223,6 +224,30 @@ class TwigExtension extends \Twig_Extension {
}
}
/**
* Gets the built and processed entity form for the given entity.
*
* @param string $entity_type
* The entity type.
* @param mixed $id
* The ID of the entity to build.
* @param string $operation
* (optional) The operation identifying the form variation to be returned.
* @param bool $check_access
* (Optional) Indicates that access check is required.
*
* @return array
* The processed form for the given entity and operation.
*/
public function drupalEntityForm($entity_type, $id = NULL, $operation = 'default', $check_access = TRUE) {
$entity = $id
? \Drupal::entityTypeManager()->getStorage($entity_type)->load($id)
: \Drupal::routeMatch()->getParameter($entity_type);
if ($entity && (!$check_access || $entity->access('update'))) {
return \Drupal::service('entity.form_builder')->getForm($entity, $operation);
}
}
/**
* Returns the render array for a single entity field.
*

10
tests/src/Functional/TwigTweakTest.php

@ -7,6 +7,7 @@ use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\responsive_image\Entity\ResponsiveImageStyle;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
/**
* A test for Twig extension.
@ -52,6 +53,9 @@ class TwigTweakTest extends BrowserTestBase {
'label' => 'Example',
'breakpoint_group' => 'responsive_image',
])->save();
// This is required for testing entity form.
$this->grantPermissions(Role::load(Role::ANONYMOUS_ID), ['edit any page content']);
}
/**
@ -128,6 +132,12 @@ class TwigTweakTest extends BrowserTestBase {
$xpath .= '/h2/a/span[text() = "Beta"]';
$this->assertByXpath($xpath);
// Test entity form.
$xpath = '//div[@class = "tt-entity-form"]/form';
$xpath .= '//input[@name = "title[0][value]" and @value = "Alpha"]';
$xpath .= '/../../../div/input[@type = "submit" and @value = "Save"]';
$this->assertByXpath($xpath);
// Test field.
$xpath = '//div[@class = "tt-field"]/div[contains(@class, "field--name-body")]/p[text() != ""]';
$this->assertByXpath($xpath);

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

@ -27,6 +27,7 @@
<div class="tt-entity-default">{{ drupal_entity('node', 1) }}</div>
<div class="tt-entity-teaser">{{ drupal_entity('node', 1, 'teaser') }}</div>
<div class="tt-entity-from-url">{{ drupal_entity('node') }}</div>
<div class="tt-entity-form">{{ drupal_entity_form('node', 1) }}</div>
<div class="tt-field">{{ drupal_field('body', 'node', 1) }}</div>
<div class="tt-menu-default">{{ drupal_menu('twig-tweak-test') }}</div>
<div class="tt-menu-level">{{ drupal_menu('twig-tweak-test', 2) }}</div>
@ -45,7 +46,7 @@
<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-link-access">{{ drupal_link('Administration', 'admin', {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>
<div class="tt-image-style">{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}</div>

Loading…
Cancel
Save