Download & Extend

DX: Allow ordering of #validate and #submit callbacks

Project:Drupal core
Version:8.x-dev
Component:forms system
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Issue tags:DX (Developer Experience)

Issue Summary

In the past week, I have had two projects where I needed to use hook_form_alter() to add a submit handler to a form. Problem is, I need the submit handler to fire in a particular order with regard to both the base form and any other hook_form_alter() implementations.

Right now, AFAIK, #submit is just a positional array, and you have to do some nasty sorting of that array in order to manipulate the execution order of your handler.

Proposal is to add an #order or #execution_order parameter to the #submit element, so that we can fine-tune the sort on these elements.

E.g.

$form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Submit'),
  '#order' => 0, // defaults to zero if not set
);

$form['#submit'][] = array(
  '#callback' => 'mycallback_submit',
  '#order' => -5, // We always runs before anything else!
);

We would then modify form_execute_handlers() to sort based on this value.

Comments

#1

To be consistent with the rest of the FAPI array, #order should probably be #weight.

#2

Can't use #weight here, since that is already taken, though it would be nice.

#3

Version:7.x-dev» 8.x-dev

#4

#weight still can be used in #submit arrays, like

<?php
if (!empty($node->nid) && node_access('delete', $node)) {
$form['actions']['delete'] = array(
 
'#type' => 'submit',
 
'#value' => t('Delete'),
 
'#weight' => 15,
 
'#submit' => array(array('#weight' => -50, '#callback' => 'node_form_delete_submit')),
);
?>

no?

#5

It can? Is that documented somewhere?

I'd be ok with a docs change if that is the case.

#6

No, I just meant that the word 'weight' is not reserved, and can be used

#7

I see. I figure using #weight in that case might be confusing for people scanning the API.

nobody click here