drupal_execute Gotcha In Drupal 6.x

Photo of Greg Harvey
Wed, 2008-08-27 18:24By greg

Many of you are probably familiar with drupal_execute(). However, those familiar with it from Drupal 5.x days will have to watch a new feature in Drupal 6.x.

Here's an example of an item in a hook_menu() implementation in the Taxonomy module for Drupal 6.x:

$items['admin/content/taxonomy/add/vocabulary'] = array(
'title' => 'Add vocabulary',
'page callback' => 'drupal_get_form',
'page arguments' => array('taxonomy_form_vocabulary'),
'access arguments' => array('administer taxonomy'),
'type' => MENU_LOCAL_TASK,
'parent' => 'admin/content/taxonomy',
'file' => 'taxonomy.admin.inc',
);
?>

Note the last item in the array, 'file'. This allows you to move the function(s) required by this menu item's page callback function in to a separate include file. By including the 'file' item in the array, you tell the Drupal core menu functions to include that file before executing on the callback.

That's great, but it impacts upon drupal_execute() because it has no idea about this include file. In Drupal 5.x this would've been fine:

//build the taxonomy form for this vocab
$this_form['values']['name'] = 'Free tags';
$this_form['values']['description'] = '';
$this_form['values']['help'] = '';
$this_form['values']['nodes'] = array(
'page' => 'page',
'story' => 'story',
);
$this_form['values']['tags'] = 1;

//submit form
drupal_execute('taxonomy_form_vocabulary', $this_form);
?>

But in Drupal 6.x the same code will result in a warning something like this:

warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'taxonomy_form_vocabulary' was given in C:\projects\boston\www\includes\form.inc on line 366.

The problem is drupal_execute() tries to submit the form without knowing the required functions are in the 'taxonomy.admin.inc' file, specified in the hook_menu() for the Taxonomy module. There is more about it here:
http://drupal.org/node/201739

There is scope to make drupal_execute() smarter here, but in the meantime, if you see funny warnings on a drupal_execute(), the chances are an include is not being included. In the case of adding a taxonomy vocabulary, the correct Drupal 6.x code looks like this:

//build the taxonomy form for this vocab
$this_form['values']['name'] = 'Free tags';
$this_form['values']['description'] = '';
$this_form['values']['help'] = '';
$this_form['values']['nodes'] = array(
'page' => 'page',
'story' => 'story',
);
$this_form['values']['tags'] = 1;

//include the file containing the required functions
module_load_include('inc', 'taxonomy', 'taxonomy.admin');

//submit form
drupal_execute('taxonomy_form_vocabulary', $this_form);
?>