Custom code breaks after upgrade to v8.x-1.8

Created on 28 June 2024, 5 months ago
Updated 5 July 2024, 5 months ago

Problem/Motivation

I have a workflow field on a paragraph entity. After upgrading from Workflow 8.x-1.7 to 1.8, some of my existing code is broken.

First problem: In a hook_form_alter(), I want to fetch the initial value in the workflow widget. I'm doing this by looking in the $form variable, drilling down to [<statefieldname>]['widget']['0']['to_sid']['#default_value']. But in v1.8 this no longer works, and instead I have to go deeper, down to [<statefieldname>]['widget']['0']['to_sid']['widget']['0']['target_id']['#default_value'].

Second problem: In a form validation callback routine, I want to check the submitted value in the workflow widget. I'm doing this by looking at $form_state->getValue([<parentfield>])[<statefieldname>][0]['to_sid']. But in v1.8, this no longer works - I get Undefined array key "<statefieldname>".

Is there a better way to access the values in a workflow widget? One which doesnt involve long, complicated access paths which (I now discover) can change from one version of workflow to the next?

The code was originally written for Drupal 8. It was successfully migrated to D9 and D10. I'm now running this with Drupal 10.3.0. My code works fine when I downgrade Workflow back to 1.7, so I know the problem is related to Workflow, not to core or any other module.

💬 Support request
Status

Active

Version

1.8

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @dabley
  • 🇳🇱Netherlands johnv

    Indeed, the 'internal' interface was changed, in order to re-use the standard 'Manage Form Display' settings of the Workflow subfields.
    Your changes are correct.
    Please check workflow.api.php file for more useage.
    also, $transition = $form_state->{$field_name} ->getTransition(); was introduced.

  • Hi @johnv, thanks for your quick response, and for confirming that my solution to my first problem is correct. But for my second problem, I dont have a solution. When I call $form_state->getValue([<parentfield>]), I get back an array which includes all the fields on
    the form, but excluding field_state, which holds my workflow. In v1.7, field_state was there alongside all the other fields, but with v1.8, it is simply gone.

    I looked in workflow.api.php file, but I couldn't see anything that looked relevant.

    Where can I find my field_state inside $form_state?

  • 🇳🇱Netherlands johnv

    Indeed, nothing in $form_state. Strange.
    You can check $form[$field_name].

  • Hi @johnv, yes, the workflow field (field_state) does appear in the $form array - but this is the value originally shown on the edit form, not the value after the user has made changes.

    I have now found the workflow field in the $form_state - but it is not where I'd expected. Instead of
    $to_state = $form_state->getValue([<parentfield>, 'field_state', 'to_sid', 0, 'target_id']);
    it is in
    $to_state = $form_state->getValue(['to_sid', 0, 'target_id']);

    So, I can look up the value. But I also want to be able to update the value in my validation routine. However, when I try:
    $form_state->setValue(['to_sid', 0, 'target_id'], $new_value);
    I find that the value in the entity doesnt actually get updated.

    Not only that, but simply trying to change the workflow state using the drop-down on the edit page (rather than my custom code), the field also does not get updated.

    This looks like a bug to me.

Production build 0.71.5 2024