Unverified Commit 5a6f2954 authored by alexpott's avatar alexpott

Issue #2899248 by Sam152: Don't reimplement validation rules for workflow...

Issue #2899248 by Sam152: Don't reimplement validation rules for workflow state add/edit forms in ::copyFormValuesToEntity
parent 7c6f4aec
...@@ -116,15 +116,13 @@ public function exists($state_id) { ...@@ -116,15 +116,13 @@ public function exists($state_id) {
* The current state of the form. * The current state of the form.
*/ */
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) { 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 */ /** @var \Drupal\workflows\WorkflowInterface $entity */
$values = $form_state->getValues(); $values = $form_state->getValues();
$type_plugin = $entity->getTypePlugin(); $entity->getTypePlugin()->addState($values['id'], $values['label']);
// 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']);
}
} }
/** /**
......
...@@ -161,6 +161,10 @@ public function form(array $form, FormStateInterface $form_state) { ...@@ -161,6 +161,10 @@ public function form(array $form, FormStateInterface $form_state) {
* The current state of the form. * The current state of the form.
*/ */
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) { 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 */ /** @var \Drupal\workflows\WorkflowInterface $entity */
$values = $form_state->getValues(); $values = $form_state->getValues();
$entity->getTypePlugin()->setStateLabel($values['id'], $values['label']); $entity->getTypePlugin()->setStateLabel($values['id'], $values['label']);
......
...@@ -90,12 +90,19 @@ public function testStateMachineNameValidation() { ...@@ -90,12 +90,19 @@ public function testStateMachineNameValidation() {
])->save(); ])->save();
$this->drupalLogin($this->createUser(['administer workflows'])); $this->drupalLogin($this->createUser(['administer workflows']));
$this->drupalPostForm('admin/config/workflow/workflows/manage/test_workflow/add_state', [ $this->drupalPostForm('admin/config/workflow/workflows/manage/test_workflow/add_state', [
'label' => 'Test State', 'label' => 'Test State',
'id' => 'Invalid ID', 'id' => 'Invalid ID',
], 'Save'); ], 'Save');
$this->assertSession()->statusCodeEquals(200); $this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('The machine-readable name must contain only lowercase letters, numbers, and underscores.'); $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.');
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment