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.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Comments

keshavv created an issue. See original summary.

keshavv’s picture

Issue summary: View changes
keshavv’s picture

Issue summary: View changes
johnv’s picture

I need to check this.
So, it is not sufficient to set the button label in the Workflow config 'Transition label' settings?

johnv’s picture

Status: Active » Postponed (maintainer needs more info)

Perhaps this has to do with the new WorkflowHooks class. There, the call to hook_form_alter is implemented, but empty:

  #[Hook('form_alter')]
  public function formAlter(&$form, FormStateInterface $form_state, $form_id) {
    // @todo D12: remove this hook. It is now done in WorkflowTransitionElement.
    // WorkflowTransitionButtons::addActionButtons($form, $form_state, $form);
  }

It still contains $form['actions'].

In file workflow.module, you'll find a 'legacy hook', that calls above class:

#[LegacyHook]
function workflow_form_alter(&$form, FormStateInterface $form_state, $form_id): void {
  // Keep aligned: workflow_form_alter(), WorkflowTransitionForm::actions().
  \Drupal::service(WorkflowHooks::class)->formAlter($form, $form_state, $form_id);
}

After update.php, you should be able to examine those pieces of code.

keshavv’s picture

Hello 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']?

johnv’s picture

I see what happens:
- first, Drupal\workflow\Hook\WorkflowHooks->formAlter is called;
- then, addActionButtons is called, which adds the buttons and sets the labels.

So the hook is too soon.

addActionButtons() is called here:

  public static function processTransition(array &$element, FormStateInterface $form_state, array &$complete_form): array {
    // Override WorkflowTransitionElement baseFields, created by Field UI.
    WorkflowTransitionElement::alter($element, $form_state, $complete_form);
    WorkflowTransitionButtons::addActionButtons($element, $form_state, $complete_form);
    return $element;
  }

No hooks are available anymore in FormBuilder->DoBuildForm, but there is an afterBUild:

  // The #after_build flag allows any piece of a form to be altered
    // after normal input parsing has been completed.
    if (isset($element['#after_build']) && !isset($element['#after_build_done'])) {
      foreach ($element['#after_build'] as $callback) {
        $element = call_user_func_array($form_state->prepareCallback($callback), [$element, &$form_state]);
      }
      $element['#after_build_done'] = TRUE;
    }

Please try that. I can provide an afterbuild in a next version, that calls a workflow_formelement_afterbuild hook in workflow.api.php.

johnv’s picture

Hmm, 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.

kubokura’s picture

Hi, 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.

cp -i [your path]/drupal/web/core/modules/system/templates/input.html.twig [your custom theme path]/templates/input--submit.html.twig
vi input--submit.html.twig

{#
/**
 * @file
 * Default theme implementation for an 'input' #type form element.
 *
 * Available variables:
 * - attributes: A list of HTML attributes for the input element.
 * - children: Optional additional rendered elements.
 *
 * @see\Drupal\Core\Form\FormPreprocess::preprocessInput()
 *
 * @ingroup themeable
 */
#}
{#
{{ dump(attributes.addClass(classes).id|render) }}
#}
{% if attributes.addClass(classes).id|render == [your element id of the button] %}
  <input{{ attributes.setAttribute('value', 'new_name') }} />{{ children }}
{% else %}
  <input{{ attributes }} />{{ children }}
{% endif %}

  • johnv committed 6571bbd5 on 2.1.x
    Issue #3569159: Fix regression- Unable to update form using...
johnv’s picture

Version: 2.2.2 » 2.1.10
Status: Postponed (maintainer needs more info) » Fixed

I 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.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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

johnv’s picture

Status: Closed (fixed) » Active
johnv’s picture

It 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.

johnv’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.