How to programmatically access custom field in workflow transition

Created on 23 March 2024, 11 months ago
Updated 11 April 2024, 11 months ago

Problem/Motivation

In my workflow, I have defined a custom field to the workflow form.
I would like to perform validation of the transition based on the value of this field in the hook_workflow 'transition pre'.
However, I struggle figuring out how do I access the value of the field from the supplied WorkflowTransitionInterface.

Any suggestions are greatly appreciated.

πŸ’¬ Support request
Status

Fixed

Version

1.7

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States mbabayan

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

Comments & Activities

  • Issue created by @mbabayan
  • πŸ‡³πŸ‡±Netherlands johnv

    Please check workflow.api.php.
    When you enable workflow_devel module, each of the hooks in workflow.apiphp is called.
    The $transition object contains multiple gettes and setters, as per the WorkflowTransitionInterface.
    E.g., $transition->getTargetEntity() gives the main entity. $transition->getToSid() and getToState contain the new Stae and StateID.

  • πŸ‡³πŸ‡±Netherlands johnv

    Or you can use the 'magic' functions:
    - $value = $transition->{$field_name}->value;
    - $value = $transition->{$field_name}->__get('foo');
    - $transition->{$field_name} = $values[$field_name]; using form_state->values()

  • πŸ‡ΊπŸ‡ΈUnited States mbabayan

    With apologies, I'm still stuck.

    I understand how I can get to the transition object. I can also get the "To" State via $transition->getToState().
    How do I get a value of the field defined on the workflow form from there? E.g. the field system name is "field_reassign_to".

    $transition->getToState()->get("field_reassign_to") does not return anything, even though the transition is performed with this field populated in the workflow form.

    Thank you!

  • πŸ‡³πŸ‡±Netherlands johnv

    Your field is not in the state, but on the transition.
    (Indeed, the naming of the added field 'Workflow State' may be confusing)

    So, try:

    $field_name = 'field_reassign_to'
    $transition->{$field_name}->value;
    or
    $transition->get{$field_name}->value;
    

    Thisis like any entity with fields added via field UI.

  • Status changed to Fixed 11 months ago
  • πŸ‡ΊπŸ‡ΈUnited States mbabayan

    Thank you, got it working!

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

Production build 0.71.5 2024