Problem/Motivation
I need to add CSS classes and update the button label programmatically using hook_form_alter(). However, the workflow transition buttons are no longer available under $form['actions'].
The button renders correctly in the template using
{{ element.actions.workflow_project_workflow_phase_1 }},
but I’m unable to target it via
$form['actions']['workflow_project_workflow_phase_1'].
Additionally, even the default action buttons are being overridden. This is preventing us from targeting or modifying the workflow transition buttons programmatically, as they are no longer available under $form['actions']
This was working as expected in the Workflow module version 2.2.0, but the behavior has changed after upgrading.
Comments
Comment #2
keshavv commentedComment #3
keshavv commentedComment #4
johnvI need to check this.
So, it is not sufficient to set the button label in the Workflow config 'Transition label' settings?
Comment #5
johnvPerhaps this has to do with the new WorkflowHooks class. There, the call to hook_form_alter is implemented, but empty:
It still contains $form['actions'].
In file workflow.module, you'll find a 'legacy hook', that calls above class:
After update.php, you should be able to examine those pieces of code.
Comment #6
keshavv commentedHello Johnv, thanks for the update.
We have a lot of custom code that modifies the workflow button based on various conditions. Would it be possible to adjust it via $form['actions']?
Comment #7
johnvI see what happens:
- first,
Drupal\workflow\Hook\WorkflowHooks->formAlteris called;- then,
addActionButtonsis called, which adds the buttons and sets the labels.So the hook is too soon.
addActionButtons() is called here:
No hooks are available anymore in FormBuilder->DoBuildForm, but there is an afterBUild:
Please try that. I can provide an afterbuild in a next version, that calls a workflow_formelement_afterbuild hook in workflow.api.php.
Comment #8
johnvHmm, the #afterbuild - which is implemented - does not pass the $complete_form, so you cannot update the $form actions.
Indeed, the function WorkflowTransitionButtons::addActionButtons() is moved from WorkflowHooks::formAlter() to WorkflowTranstionElement::processTransition(), which seemed more performant. But is wrecks your code.
You could comment the latter and uncomment the first call.
Comment #9
kubokura commentedHi, keshavv, I have had a similar experience regarding setting the Comment sub-field to required/optional programmatically when upgrading from version 2.1.7 to 2.2.2.
If you have custom theme, you may want to write your input--submit.html.twig like below at this moment.
Comment #11
johnvI see, by changing the order, normal hooks are overridden.
Above commit restores the order of the hooks.
The following hooks (and others) should now work as before:
- #[Hook('field_widget_single_element_workflow_default_form_alter')] , declared in WorkflowFieldHooks;
- #[Hook('form_alter')].
Please test dev version, and report back. I will then release a new version 2.1.11 (!) shortly.
Comment #14
johnvComment #15
johnvIt is noticed that after the last commit, the hook_alter was not executed on the standalone Form (in Block, etc.) anymore.
It is fixed with this commit.
Comment #16
johnv