Closed (fixed)
Project:
Drupal core
Version:
8.4.x-dev
Component:
workflows.module
Priority:
Major
Category:
Bug report
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
15 Jun 2017 at 16:37 UTC
Updated:
2 Oct 2017 at 11:09 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
manuel garcia commentedThis also happens if you add a transition.
If you do happen to create a state or transition with a numeric machine name then the workflow edit page just shows you a blank page:
The website encountered an unexpected error. Please try again later.Adding a workflow with a numeric machine name is fine.
Comment #3
timmillwoodThis is because the same sorting is done in
\Drupal\workflows\Entity\Workflow::getTransitions.Comment #4
timmillwoodSo going with the solution of, instead of making this work, prevent numeric only machine names for states and transitions.
In doing this I found the exceptions we throw in addState and addTransition actually prevent the validation from running, so fixing that too.
Comment #5
manuel garcia commentedLooks good @timmillwood - this currently prevents the user from breaking things.
We'll need to update this text in the add forms and the tests to reflect the new restriction.
Comment #6
timmillwoodUpdated the error text.
Comment #7
manuel garcia commentedThanks @timmillwood, errors look fine now.
We should change the
#descriptionfor the Machine-readable name fields as well:Comment #8
timmillwoodUpdating machine-name description:

Comment #9
manuel garcia commentedPerfect thanks @timmillwood, from my perspective this is good to go now.
Comment #10
tstoecklerSo the try-catch is explained in #4 with:
Not 100% sure about this, though. Over in #2842193: Exception in Workflow::addState when an invalid machine name is given we came to the agreement, that, although it is ugly, we should explicitly duplicate the validation
::copyFormValuesToEntity(). This seems to go against that. I am definitely against it, but even if we do go with a try-catch:\Exception. If the database server dies in the middle of the request,it should not be handled equivalently to someone inputting an invalid value in a form.
Marking needs review, though, to get some more opinions on this.
Comment #11
manuel garcia commentedThanks @tstoeckler for the review - tackling #10.1 and #10.2 here.
Comment #12
tstoecklerThis can also be removed then.
Again, though, I am not endorsing the try-catch. I would actually prefer duplicating the validation. But let's hear what others think.
Comment #13
manuel garcia commentedArgh - good catch... removing the comments as well.
Yup, we should have more input on this... just making the changes you suggested because they make sense, in case we do want to go the try/catch route =)
Comment #14
sam152 commentedComment #15
timmillwoodIn #2830584: Use modals for creating, updating, and deleting workflows, with a new DialogFormTrait it was agreed to go with the try/catch, so it looks like this one can go back to RTBC again.
Comment #17
manuel garcia commentedComment #18
vj commentedPatch rerolled
Comment #20
timmillwoodFixing broken tests.
Comment #22
vj commented@timmillwood Thanks for fixing tests.
Patch re-rolled.
Comment #23
manuel garcia commentedLooking good!
#2849827: Move workflow "settings" setters and getters to WorkflowTypeInterface has landed now :) - Im guessing this affects
WorkflowTransitionAddFormas well.Comment #24
sam152 commentedComment #26
sam152 commentedI think we should explore supporting integer IDs, any reason we abandon that so quickly? Seems like it'd be good for consistency, especially given even the workflow entity itself can have numeric IDs. I've opened #2899187: For consistency, workflow states and transitions should support numeric machine names to have a look at this.
One thing I found looking into this is that we previously took the approach in #2842193: Exception in Workflow::addState when an invalid machine name is given to duplicate the machine name validation logic in
copyFormValuesToEntityso we didn't trigger the exception. Why didn't we apply the same treatment to transitions? Because the transition forms already had a pretty good solution for this.I think we should slightly rejig things, so this issue becomes "Make validation in transition and state forms consistent" and the prior issue I linked dedicated to making ID based machine name handling consistent. That way we avoid our snow-flake error messages about not containing only numbers etc.
If we decide to do that, here is a patch which introduces that consistency. Proposed issue name:
Comment #27
timmillwood@Sam152 With #26 we still get the same issue which stems from \Drupal\workflows\Plugin\WorkflowTypeBase::labelWeightMultisort not sorting them correctly.
I think we need a separate issue to not reimplement the validation, rather than changing the scope of this one. This should focus on the issue of using integers for state and transition IDs.
Changing the validation to prevent integers IDs is a good first step, we could then have a follow up to fix the sorting, thus allow integer IDs again.
Comment #28
sam152 commentedAh, I logged #2899187: For consistency, workflow states and transitions should support numeric machine names to sort the numeric IDs, but makes sense to use this issue as well.
Comment #29
sam152 commentedI've logged #2899248: Don't reimplement validation rules for workflow state add/edit forms in ::copyFormValuesToEntity to capture the validation rejig, looking into numeric IDs now.
Comment #30
sam152 commentedAttached brings id handling into line, by allowing numeric IDs.
Comment #31
timmillwoodManually tested, seems to work perfectly!
Thanks @Sam152
Comment #32
sam152 commentedRemoving screenshot of the error message introduced in earlier patches from the IS.
Comment #34
xjmThis is kind of weird formatting for the parameters; it doesn't really follow our coding standards. Either the parameters should be all on one line, or they should be each on a separate line. I'd suggest the former.
Also, let's add inline comments explaining the sort logic, since it's totally not obvious by reading the method itself why we're doing this. The test coverage proves the fix nicely, but we should also make it clear in the code.
Thanks!
I think this is a patch-safe backport as it should be BC for the normal non-integer ID case.
Comment #35
timmillwoodI've opened an issue #2900700: Add orderby to content_moderation schema sequences to order the states and transitions in the config object, but this is not to be confused by the sorting for display and use within Drupal, which is what this issue is fixing.
Changing the formatting and adding some inline comments as @xjm suggests in #34.
Moving back to RTBC as there are no code changes.
Comment #36
vj commentedTested patch #35. Tested add new state & add new transition, works without any error.
Comment #37
wim leersIndeed, #2900700: Add orderby to content_moderation schema sequences is conceptually related, but very different.
Comment #39
timmillwoodComment #40
xjmPromoting to major according to the criteria in https://www.drupal.org/core/issue-priority#major-bug; this results in a fatal error and renders the workflow module unusable based on actions taken only in the UI and does not have a workaround.
Comment #41
xjmSo a couple of concerns here still:
The test-only patch doesn't show the fatal error; it just shows the invalid sort. This is presumably because it's a unit test. This means we don't have any test coverage exercising the case where the fatal error is thrown.
Even with the added comments I'm still struggling to see how the fatal is triggered and how the change fixes it; this could be a combination of
array_multisort()being evil and insufficient caffeine.Edit: Left at RTBC because this is most likely still a me-problem.
Comment #42
xjmOkay, so part of my confusion is with the documentation of
labelWeightMultisort(). This method is passed a list of state or transition keyed by state or transition ID, but the documentation for the method does not suggest a meaningful key/value association.I'm wondering if we need a deeper fix; I keep coming back to words like "brittle" and "code smell". Going to get a second opinion since it might be worthwhile to fix the major with the quickfix here and then improve it more in a followup, except I haven't quite decided yet what that followup would be exactly so it's not very actionable feedback.
Thanks, and sorry for the delay reviewing this and other issues.
Comment #43
xjmOkay, discussed with @catch; we agreed it at least needs an additional functional test case that exercises the fatal. The inline comments also could spell out what's happening a bit more explicitly; always a good idea when there's lots of array manipulation.
I did manually test and confirm that this patch does indeed fix the fatal.
I realize now why the array was formatted as it was in #30; it was grouping each array with its flags with the array_multisort(). Sorry for the kneejerk about that. While I think it may violate coding standards, it also does make the function bit more readable. I also realized it was out of scope, because the array is already sorted this way. So we can revert those changes and it was a mistake on my part to push back on them here.
This is reminding me of all the reasons I hate
array_multisort(). :PAnother thing I'd want to see (probably also out of scope) is fixing the documentation for this method to indicate that the key/value association is important. However, that plus the need for inline comments to explain what's going wrong (the current comments still aren't quite enough for me to understand what's going to break just by reading the code) plus the existence of the bug in the first place makes me wonder if these methods shouldn't just be refactored. I don't have a concrete suggestion right now on how to improve them, and I don't want to block what is a nasty major on a vague request for refactoring, but maybe @Sam152 or @timmillwood has ideas? Are there past issues related to this?
Comment #44
timmillwoodI think adding a functional test for creating a state and transition with an numeric machine name and label is a reasonable request. Then we can have a follow up to look at other sorting options.
I'm not going to get to this today, but will take a look in the morning unless @Sam152 or anyone else takes a look first.
Comment #45
timmillwoodWorking on a test for this.
Comment #46
timmillwoodHere's a UI test to test creating a workflow with a numeric ID and label, with a state and a transition with a numeric ID and label.
Also reverted the array_multisort(), updated some comments, and fixed empty ComplexTestType forms.
Comment #47
timmillwoodAdding the interdiff from #46 as a test-only patch, and re-uploading the real patch from #46 so the latest patch is still the correct one.
Comment #49
timmillwoodLet's try that again, I was diffing my 8.5.x codebase against 8.4.x, oops!
Comment #51
timmillwoodLooks like the issue only surfaces with two or more states.
Comment #53
xjmSo
$labelsis never used again after this... why is it there?Edit: Because it's the secondary sort for the weights which we do return, disregard. But really inline comments would be good.
Comment #54
timmillwoodBecause if there are two with the same weight it then sorts by label?
Comment #55
xjm@timmillwood, yeah, saw that on second read; just edited my comment. Sorry, I hate
array_multisort(). :P Inline comments would really help. Like:And there should be test cases for sorting with duplicate weights, duplicate labels, etc.
Comment #56
xjmPosted #2906879: WorkflowTypeBase:: labelWeightMultisort() has meaningful param/return keys, but docs do not indicate this for the out-of-scope improvements I suggested for the docblock.
Comment #57
timmillwoodHere we are @xjm, added your comment suggestions, and a test to test the sorting.
Comment #58
xjmMinor issue here with the comment being over the 80 char line.
On a quick scan, the added tests and comments look great. Thanks @timmillwood.
Comment #59
timmillwoodServes me right for just copying and pasting from #55.
Comment #61
timmillwoodComment #62
xjmAlright, this looks good to me. Thanks @timmillwood for adding the tests. I keep thinking there must be a better way but that's not actually helpful and no reason to hold back a major bugfix.
Comment #65
catchNo comments other than what xjm said in #62, so:
Committed/pushed to 8.5.x and cherry-picked to 8.4.x. Thanks!