Changing the hook execution sequence of form hook.

  • 19 April 2014
  • mohit.aghera

 

Generally in drupal modules are executed in as per the weight in the specified by the system table. 

If two module has the same weight then modules are executed alphabetical order based on the name of module. 

 
During working on one of my project, I was working with workflow module. 

New version of workflow module adds Target State radio buttons on node form. I wanted to move these radio buttons to vertical tabs in node creation form. 

I created a module for the form_alter function. I named my module as my_module.module. Now when I try to get the $form variable in hook_form_atler , the workflow form was not being added in the $form array. Reason was that my form alter was being called first and then workflow modules form alter. 

 
Workflow module adds target state radio buttons by using hook_form_alter(). So based on suggestion of my colleague I checked the system mdoule and checked the weight of my custom module and workflow module. Both modules were having the same weight. So naturally my custom module's form alter was being called first and then workflow module's form alter. 

To fix this I tried two solutions: 

1. Increase the weight of my custom module, so that it runs last. 

To increate the weight I have to write the following snippet in my module's .install file. 

<?php
function your_module_name_install() {
db_update('system')
->fields(array('weight' => 1500))
->condition('name', '[your_module_name]', '=')
->execute();
}
?>

 
I chagned the weight to maximum so that my custom form alter will run last. I reinstalled the module and it worked as expected. 

 
2. Second solutions is about changing the hook_form_alter() execution sequence. 

We have to alter form alter sequence so that I runs last in my module. https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_module_implements_alter/7 

 
This hook is invoked during module_implements(). A module may implement this hook in order to reorder the implementing modules, which are otherwise ordered by the module's system weight. 

So I have written following code in my_module.module file. 

/**

* Implements hook_module_implements_alter().

*/

function my_module_module_implements_alter(&$implementations, $hook) {

if ($hook == 'form_alter') {

$group = $implementations['my_module'];

unset($implementations['my_module']);

$implementations['my_module'] = $group;

}

}

/**

* Implements hook_form_alter().

*/

function my_module_form_alter(&$form, &$form_state, $form_id) {

$node_types = array('article_node_form');

if(in_array($form_id, $node_types)) {

$form['workflow_tab'] = array(

'#type' => 'fieldset',

'#title' => t('Workflow'),

'#collapsible' => TRUE,

'#collapsed' => FALSE,

'#group' => 'additional_settings',

'#weight' => 7,

'#description' => t('Workflow state settings')

);

$form['workflow_tab'][LANGUAGE_NONE][0] = $form['workflow'];

unset($form['workflow']);

}

}

 
 

Credits: Hussain Abbas (for guidance) https://twitter.com/hussainweb 

Drupal Stackoverflow question: http://drupal.stackexchange.com/questions/7816/hook-form-alter-execution-order/9720#9720 

 

Comments

Thanks for sharing your thoughts about money market ira. Regards

coffee's picture

Hello, i read your blog occasionally and i own a similar one and i was just curious if you
get a lot of spam remarks? If so how do you stop it, any plugin or anything
you can suggest? I get so much lately it's driving me crazy so any support is very much appreciated.

Thank you for every other informative blog. The place ele may just I am getting
that kind of information written in ssuch an ideal method?
I have a venture that I am simply now operating on, andd I have
been at the look out for suchh information.

Andrew's picture

What's Going down i am new to this, I stumbled upon this I have
found It absolutely useful and it has aided me ouut loads.
I'm hoping to give a contribution & help other customers lik its aided me.
Great job.

Heya just wanted to give you a brief herads up aand let yoou know a few of the
pictures aren't loading correctly. I'm not sure why but I think its a linking issue.I've tried it in two different
web browses and both show the same results.

timmluca's picture

The various situations that we never even heard of. Really love these kinda articles. behance

Prediski's picture

this is my blog http://diprediksi.drupalgardens.com/ on drupalgarden, how to add slide show ?

jaydenmatthew15's picture

 

thank you the Article is very Good

amazon.com

Add new comment