diff --git a/src/TwigExtension.php b/src/TwigExtension.php index da7d328..4cb623e 100644 --- a/src/TwigExtension.php +++ b/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); } } diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index e178b8e..d16b35f 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/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. + } + } 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 a2ab787..ac6f42f 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -27,7 +27,8 @@
{{ drupal_entity('node', 1) }}
{{ drupal_entity('node', 1, 'teaser') }}
{{ drupal_entity('node') }}
-
{{ drupal_entity_form('node', 1) }}
+
{{ drupal_entity_form('node', values={type: 'page'}) }}
+
{{ drupal_entity_form('node', 1) }}
{{ drupal_field('body', 'node', 1) }}
{{ drupal_menu('twig-tweak-test') }}
{{ drupal_menu('twig-tweak-test', 2) }}