diff --git a/core/modules/workflows/src/Form/WorkflowStateAddForm.php b/core/modules/workflows/src/Form/WorkflowStateAddForm.php index 5eb1b9a4c76822d94672b2effe4e96c22bb08a2d..1abc8e3df734dffddbe463c8766f2be9ac9a4c71 100644 --- a/core/modules/workflows/src/Form/WorkflowStateAddForm.php +++ b/core/modules/workflows/src/Form/WorkflowStateAddForm.php @@ -116,15 +116,13 @@ public function exists($state_id) { * The current state of the form. */ protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) { + if (!$form_state->isValidationComplete()) { + // Only do something once form validation is complete. + return; + } /** @var \Drupal\workflows\WorkflowInterface $entity */ $values = $form_state->getValues(); - $type_plugin = $entity->getTypePlugin(); - - // Replicate the validation that Workflow::addState() does internally as the - // form values have not been validated at this point. - if (!$type_plugin->hasState($values['id']) && !preg_match('/[^a-z0-9_]+/', $values['id'])) { - $type_plugin->addState($values['id'], $values['label']); - } + $entity->getTypePlugin()->addState($values['id'], $values['label']); } /** diff --git a/core/modules/workflows/src/Form/WorkflowStateEditForm.php b/core/modules/workflows/src/Form/WorkflowStateEditForm.php index 4fe5f5e55753b7b2768a2d0dd473dd85f58c9f16..7b8b90355ebffa372e9fea7168d7575e0bbc528f 100644 --- a/core/modules/workflows/src/Form/WorkflowStateEditForm.php +++ b/core/modules/workflows/src/Form/WorkflowStateEditForm.php @@ -161,6 +161,10 @@ public function form(array $form, FormStateInterface $form_state) { * The current state of the form. */ protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) { + if (!$form_state->isValidationComplete()) { + // Only do something once form validation is complete. + return; + } /** @var \Drupal\workflows\WorkflowInterface $entity */ $values = $form_state->getValues(); $entity->getTypePlugin()->setStateLabel($values['id'], $values['label']); diff --git a/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php b/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php index 487e28cce0822d877f7ed9f66c7bc6c04474f169..227ddd769a0c5d31f0a091f10d77a5911d44bc3e 100644 --- a/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php +++ b/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php @@ -90,12 +90,19 @@ public function testStateMachineNameValidation() { ])->save(); $this->drupalLogin($this->createUser(['administer workflows'])); + $this->drupalPostForm('admin/config/workflow/workflows/manage/test_workflow/add_state', [ 'label' => 'Test State', 'id' => 'Invalid ID', ], 'Save'); $this->assertSession()->statusCodeEquals(200); $this->assertSession()->pageTextContains('The machine-readable name must contain only lowercase letters, numbers, and underscores.'); + + $this->drupalPostForm('admin/config/workflow/workflows/manage/test_workflow/add_transition', [ + 'label' => 'Test Transition', + 'id' => 'Invalid ID', + ], 'Save'); + $this->assertSession()->pageTextContains('The machine-readable name must contain only lowercase letters, numbers, and underscores.'); } /**