Browse Source

Added support for 'add entity' forms.

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

31
src/TwigExtension.php

@ -225,26 +225,35 @@ class TwigExtension extends \Twig_Extension {
}
/**
* Gets the built and processed entity form for the given entity.
* Gets the built and processed entity form for the given entity type.
*
* @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.
* (optional) The ID of the entity to build. If empty then new entity will
* be created.
* @param string $form_mode
* (optional) The mode identifying the form variation to be returned.
* @param array $values
* (optional) An array of values to set, keyed by property name.
* @param bool $check_access
* (Optional) Indicates that access check is required.
*
* @return array
* The processed form for the given entity and operation.
* The processed form for the given entity type and form mode.
*/
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);
public function drupalEntityForm($entity_type, $id = NULL, $form_mode = 'default', array $values = [], $check_access = TRUE) {
$entity_storage = \Drupal::entityTypeManager()->getStorage($entity_type);
if ($id) {
$entity = $entity_storage->load($id);
$operation = 'update';
}
else {
$entity = $entity_storage->create($values);
$operation = 'create';
}
if ($entity && (!$check_access || $entity->access($operation))) {
return \Drupal::service('entity.form_builder')->getForm($entity, $form_mode);
}
}

36
tests/src/Functional/TwigTweakTest.php

@ -54,8 +54,6 @@ class TwigTweakTest extends BrowserTestBase {
'breakpoint_group' => 'responsive_image',
])->save();
// This is required for testing entity form.
$this->grantPermissions(Role::load(Role::ANONYMOUS_ID), ['edit any page content']);
}
/**
@ -132,8 +130,27 @@ class TwigTweakTest extends BrowserTestBase {
$xpath .= '/h2/a/span[text() = "Beta"]';
$this->assertByXpath($xpath);
// Test entity form.
$xpath = '//div[@class = "tt-entity-form"]/form';
// Test access to entity add form.
$xpath = '//div[@class = "tt-entity-add-form"]/form';
$this->assertSession()->elementNotExists('xpath', $xpath);
// Test access to entity edit form.
$xpath = '//div[@class = "tt-entity-edit-form"]/form';
$this->assertSession()->elementNotExists('xpath', $xpath);
// Grant require permissions and test the forms again.
$permissions = ['create page content', 'edit any page content'];
$this->grantPermissions(Role::load(Role::ANONYMOUS_ID), $permissions);
$this->drupalGet('/node/2');
// Test entity add form.
$xpath = '//div[@class = "tt-entity-add-form"]/form';
$xpath .= '//input[@name = "title[0][value]" and @value = ""]';
$xpath .= '/../../../div/input[@type = "submit" and @value = "Save"]';
$this->assertByXpath($xpath);
// Test entity edit form.
$xpath = '//div[@class = "tt-entity-edit-form"]/form';
$xpath .= '//input[@name = "title[0][value]" and @value = "Alpha"]';
$xpath .= '/../../../div/input[@type = "submit" and @value = "Save"]';
$this->assertByXpath($xpath);
@ -267,4 +284,15 @@ class TwigTweakTest extends BrowserTestBase {
$this->assertSession()->elementExists('xpath', $xpath);
}
/**
* {@inheritdoc}
*/
protected function initFrontPage() {
// Intentionally empty. The parent implementation does a request to the
// front page to init cookie. This causes some troubles in rendering
// attached Twig template because page content type is not created at that
// moment. We can skip this step since this test does not rely on any
// session data.
}
}

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

@ -27,7 +27,8 @@
<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-entity-add-form">{{ drupal_entity_form('node', values={type: 'page'}) }}</div>
<div class="tt-entity-edit-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>

Loading…
Cancel
Save