Problem/Motivation

Right now, in order to save the values a user has entered, we overriding EntityForm::copyFormValuesToEntity in all of these forms:

  • WorkflowStateEditForm
  • WorkflowStateAddForm
  • WorkflowTransitionAddForm
  • WorkflowTransitionEditForm

Since the workflows is strict about the validity of the states and transitions it will add to a workflow, if you enter an invalid machine name for example, an exception is triggered which bubbles up to the user.

In #2842193: Exception in Workflow::addState when an invalid machine name is given we fixed this by reimplementing the validation provided by the machine_name element and the validateForm method in order to prevent this from happening:

  class WorkflowStateAddForm {
  ...
  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
    // 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']);
    }
  }

It seems this was only done for states however, because the transition forms had already solved this another way:

  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
    if (!$form_state->isValidationComplete()) {
      // Only do something once form validation is complete.
      return;
    }

Proposed resolution

For consistency, lets use the same strategy everywhere and at the same time make things more DRY by not reimplementing validation rules.

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#2 2899248-2.patch3.14 KBSam152
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Sam152 created an issue. See original summary.

Sam152’s picture

Status: Active » Needs review
FileSize
3.14 KB
Sam152’s picture

Title: Don't reimplement validation rules for workflow state add/edit forms in ::copyFormValuesToEntity and make them consistent with transition forms » Don't reimplement validation rules for workflow state add/edit forms in ::copyFormValuesToEntity
Issue summary: View changes
Sam152’s picture

Issue tags: +Workflow Initiative

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Sam152’s picture

Bumping for a review. Would be nice to get this clean-up in.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

This makes sense and it passes all tests. I couldn't find any other places where the duplication had happened.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work

test failed?

borisson_’s picture

Status: Needs work » Reviewed & tested by the community

1) Drupal\Tests\ComposerIntegrationTest::testComposerLockHash
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-4d747d594c115a3bccfce2bd4cde95f2
+9d37d607bfdb3f5127d18051be44fca0

That was the failure on the previous run, it's green again, setting to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 5a6f295 and pushed to 8.6.x. Thanks!

  • alexpott committed 5a6f295 on 8.6.x
    Issue #2899248 by Sam152: Don't reimplement validation rules for...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.