Comments

Sam152 created an issue. See original summary.

tacituseu’s picture

Title: Assert weights are numeric when settings state/transition weights. » [PP-1] Assert weights are numeric when settings state/transition weights.
larowlan’s picture

Title: [PP-1] Assert weights are numeric when settings state/transition weights. » Assert weights are numeric when settings state/transition weights.

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

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.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

Status: Active » Needs review
StatusFileSize
new3.05 KB

Noticed a stray quote in some related exceptions.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

The patch does exactly what's being asked for in the IS. Looks good to me.

xjm’s picture

Title: Enforce that weights are numeric when settings state/transition weights. » Enforce that weights are numeric when settings state/transition weights
+++ b/core/modules/workflows/src/Plugin/WorkflowTypeBase.php
@@ -178,7 +178,10 @@ public function setStateLabel($state_id, $label) {
+      throw new \InvalidArgumentException("The weight '$weight' is not numeric.");

@@ -390,7 +393,10 @@ public function setTransitionLabel($transition_id, $label) {
+      throw new \InvalidArgumentException("The weight '$weight' should be numeric.");

Is there any particular reason we're using different messages for the state weight vs. transition weight?

xjm’s picture

Status: Reviewed & tested by the community » Needs review

FWIW I think both could be:

The weight '$weight' must be numeric.

xjm’s picture

Status: Needs review » Needs work

Actually, ideally, the exception would also tell you which state or transition has a non-numeric weight, for better debugging.

So something like:

The weight '$weight' must be numeric for state '$label'.

Setting NW for that. Thanks!

dinesh18’s picture

Status: Needs work » Needs review
StatusFileSize
new3.1 KB
new1.27 KB

Here is an updated patch and interdiff implemented as per #10 comment.

sam152’s picture

Status: Needs review » Needs work

Hi @Dinesh18, looks good, but I think we'll have to update the tests to assert the updated exceptions.

sam152’s picture

Issue tags: +Novice
MaskOta’s picture

Status: Needs work » Needs review
StatusFileSize
new3.24 KB

My first test fixes. Please be gentle test bot.

sam152’s picture

Status: Needs review » Needs work
StatusFileSize
new2.71 KB

Hi @MaskOta, thanks for the contribution! Sometimes it's helpful to provide an interdiff, which are the changes you've introduced since the last patch. I've included one for this issue for between #11 and #14. You can generate these with git while you're working.

+    if (!is_numeric($weight)) {
+      $label = $this->getState($state_id)->label();
+      throw new \InvalidArgumentException("The weight '$weight' must be numeric for state '$label'.");
     }
...
+    if (!is_numeric($weight)) {
+      $label = $this->getTransition($transition_id)->label();
+      throw new \InvalidArgumentException("The weight '$weight' must be numeric for state '$label'.");
     }

Both these calls to getState and getTransition are guarded by a hasState/hasTransition check, so totally safe to use them for the purposes of getting the label. +1

+++ b/core/modules/workflows/src/Plugin/WorkflowTypeBase.php
@@ -390,7 +394,11 @@ public function setTransitionLabel($transition_id, $label) {
+    if (!is_numeric($weight)) {
+      $label = $this->getTransition($transition_id)->label();
+      throw new \InvalidArgumentException("The weight '$weight' must be numeric for state '$label'.");
     }

One last thing here, we're talking about transitions in this method and the exception references 'state'.

MaskOta’s picture

Status: Needs work » Needs review
StatusFileSize
new1.94 KB
new3.24 KB

Thanks for reviewing. I fixed up the labels for the transition so they make more sense.

sam152’s picture

Status: Needs review » Reviewed & tested by the community

Perfect, thanks!

larowlan’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: +DrupalSouth 2017

Committed as 7dbf76d and pushed to 8.5.x

  • larowlan committed 7dbf76d on 8.5.x
    Issue #2897134 by MaskOta, Sam152, Dinesh18, xjm: Enforce that weights...

Status: Fixed » Closed (fixed)

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