Problem/Motivation

Using custom workflows in Commerce 2.x, mglaman and I found that when transitioning to the same state, event subscribers do not fire as they should. Only happens when the 'from' and 'to' are the same value.

Use case: A "Print Order" button that would fire a PDF printer and not change the state of the order.

The field formatter displays the transition but the transition does not fire an event.

Proposed resolution

Allow a transition to fire if the source and destination state are the same

Remaining tasks

  • Write test
  • Fix bug

User interface changes

None. Actually fixes the user interface

CommentFileSizeAuthor
#9 detector.png10.34 KBzaporylie
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alexandersluiter created an issue. See original summary.

mglaman’s picture

Issue summary: View changes
mglaman’s picture

So I talked with bojanz. The real bug here is that transitions are allowed (or recognized) when there is no state value change. I do like the workaround of a transition being available to basically re-run a process... but that isn't quite how this should work.

We need to fix the formatter to, or transition validation, to consider this kind of setup and ignore them.

alexandersluiter’s picture

I guess that makes sense as far as keeping the "State Machine" a "State Machine". I will have to change my workflows file to reflect not allow non-state-change transitions then.

Is there a way to hook into the fields that it outputs and place another button in there without hooking into the state machine itself?

bojanz’s picture

Title: Event subscribers not firing during transition to same state » Don't allow transitions to the same state
Version: 8.x-1.0-beta3 » 8.x-1.x-dev

Retitling.

  • bojanz committed 8122902 on 8.x-1.x
    Issue #2951243 by bojanz: Don't allow transitions to the same state
    
bojanz’s picture

Status: Active » Fixed

Added validation.

Status: Fixed » Closed (fixed)

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

zaporylie’s picture

FileSize
10.34 KB

...4 years later 😅

Sorry for commenting on this, probably a long forgotten&fixed issue but I'm still failing to understand the reasoning as to why transition between the same state is not allowed. And I've been redirected here to search for that answer from #3058874: Allow same From/To state but cant find any clear answer,

All definitions of State Machine I know allow transitions between the same state (as a special case of transition to another state). The key features of State machine are:
- it has limited number of states
- it can be in only one state at the time
- it has defined transitions on how to reach one state from another

Some definitions will say that state machine is just like this direct graph. See the following explanation from Cornell University website:

Another way to view a state machine is as a labeled, directed graph. The states are just nodes in the graph, and the state transition function defines edges. Edges are labeled with the corresponding events. For example, we can draw the "1 detector" state machine as follows: State Machine as a graph

The definition given on Wikipedia says that "the change from one state to another is called a transition" but at the same times shows plenty of examples how transitions are allowed with from/to states being the same. This is also how the State Machines concept was explained to me back at the University.

In my head another state in this context shouldn't be interpreted as different. It is more to tell that transition requires 2 states - before and after - but doesn't limit it to before and after being actually different states.

That being said one of the popular examples of the State Machine is binary light switch with brightness control. The light can only be in 2 states: on or off but some additional (brightness) transitions are allowed.
Off can only transition to On
On can transition to Off
On can transition to On (brightness down, until off)
On can transition to On (brightness up, until 100%)

In my opinion, support for transitions between the same state should be allowed.