From a1e31e2dededadc3c9441562381aca4d7bd2af5f Mon Sep 17 00:00:00 2001 From: Vasile Chindris Date: Wed, 8 May 2024 10:56:45 +0300 Subject: [PATCH] chore(SLB-328): redirect to the entity edit form after creating the entity --- .../entity_create_split.module | 36 +++++++++++++++++++ tests/e2e/specs/drupal/preview.spec.ts | 1 - 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/drupal/entity_create_split/entity_create_split.module b/packages/drupal/entity_create_split/entity_create_split.module index 2ae2e596f..a790b295c 100644 --- a/packages/drupal/entity_create_split/entity_create_split.module +++ b/packages/drupal/entity_create_split/entity_create_split.module @@ -7,6 +7,7 @@ * the rest of the fields. */ +use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Entity\ContentEntityType; use Drupal\Core\Entity\EntityInterface; @@ -29,3 +30,38 @@ function entity_create_split_gutenberg_enabled(EntityInterface $entity) { return FALSE; } } + +/** + * Implements hook_form_alter(). + */ +function entity_create_split_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { + $formObject = $form_state->getFormObject(); + if ($formObject instanceof ContentEntityForm && $formObject->getFormDisplay($form_state)->getMode() === 'split') { + if (empty($form['#submit'])) { + $form['#submit'] = []; + } + $form['#submit'][] = 'entity_create_split_submit_redirect'; + if (!empty($form['actions']['submit']['#submit'])) { + $form['actions']['submit']['#submit'][] = 'entity_create_split_submit_redirect'; + } + } +} + +/** + * Submit handler for the entity create from, to redirect the user to the entity + * edit form. + * + * @param array $form + * @param \Drupal\Core\Form\FormStateInterface $form_state + * + * @return void + * @throws \Drupal\Core\Entity\EntityMalformedException + */ +function entity_create_split_submit_redirect(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) { + /* @var \Drupal\Core\Entity\EntityInterface $entity */ + $entity = $form_state->getFormObject()->getEntity(); + if (!empty($entity) && $entity->id()) { + $url = $entity->toUrl('edit-form'); + $form_state->setRedirectUrl($url); + } +} diff --git a/tests/e2e/specs/drupal/preview.spec.ts b/tests/e2e/specs/drupal/preview.spec.ts index 4f4f0d880..0ca958f7b 100644 --- a/tests/e2e/specs/drupal/preview.spec.ts +++ b/tests/e2e/specs/drupal/preview.spec.ts @@ -11,7 +11,6 @@ test.describe('instant preview', () => { .getByLabel('Title', { exact: true }) .fill('Instant preview test'); await page.locator('#edit-submit').click(); - await page.locator('li.tabs__tab a:text("Edit")').click(); await page .locator('#editor-edit-body-0-value h1 span') .first()