Drupal - Pass arguments using hook_menu

Your page callback is drupal_get_form, and your page argument is _mymodule_confirm_offline, so the call Drupal makes is drupal_get_form('_mymodule_confirm_offline');, which doesn't at all seem to be what you want to do.

What you need is a custom callback instead.

In hook_menu:

'page callback' => '_confirmation_form_page',
'page arguments' => array(1, 3),

And then

function _confirmation_form_page($node) {
  $question = t('Take fiche offline. ');

  return confirm_form($form, $question, 'node/' . $node->nid, t('You\'re about to depublish this fiche. <br />Are you sure you want to depublish this fiche?'), t('Yes'), t('Cancel'));
}

In order to "auto-load" an object from the url, it needs to be named, otherwise Drupal can't know how to perform the load. On a url like "node/%nid", the argument is named "nid". In reality the argument would usually be called %node, but I wanted to distinguish the parts. Drupal will then look for a function called load_nid, pass the number from the URL in, and this function is responsible for returning the fully loaded object. Core has a "load_node" function, so arguments named %node will be auto-loaded without further effort from your part.

To add autoloading into your callback then, you need

$items['node/%node/revisions/%/offline'] = array(

However, this will still only give you the right node, not the right revision. node_load() supports a second argument, $vid, that we need to use to get the right revision. Telling Drupal to pass on extra arguments is used with the load arguments key, and we need to use the fourth argument from the url node/%node/revision/%/offline for this.

So in conclusion, the hook_menu implementation becomes:

$items['node/%node/revisions/%/offline'] = array(
  'title' => 'Offline',
  'description' => 'Confirm the action "Fiche offline".',
  'page callback' => '_confirmation_form_page',
  'page arguments' => array(1),
  'load arguments' => array(3),
);
return $items;

Tags:

Routes

7