7

I would build a confirmation screen. So I initialized the path in the hook_menu as it should be but for some reason, the argument $node isn't send with the function... What am I doing wrong?

This is my hook_menu:

$items['node/%/revisions/%/offline'] = array(
      'title' => 'Offline,
      'description' => 'Confirm the action "Fiche offline".',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('_mymodule_confirm_offline'),
      'type' => MENU_LOCAL_TASK,
      'weight' => -4,
    ); 
    return $items;

And this is my function:

function _redactie_omgeving_confirm_offline($form, $form_state, $node) {
    $question = t('Take fiche offline. ');

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

But the $node-variable in my function stays empty... What do I have to do to fill in this variable?

13

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;
  • 1
    Ok, thanks! And what are the 1 and 3 about? Can I add as much items as I want? – Michiel Jan 10 '12 at 15:20
  • 2
    The 1 and 3 correspond to the arguments in 'node/%/revisions/%/offline', with 'node' being 0. This causes the content of the two %'s to be passed in to your callback. – Letharion Jan 10 '12 at 15:21
  • Ok, great! I'm learning :) How do I get a variable of a previous page in the arguments (let's say $node) – Michiel Jan 10 '12 at 15:23
  • 1
    Second update, added autoloading info. – Letharion Jan 10 '12 at 15:38
  • 1
    Wow, over a year later I accidentally came back. I've updated the code now. – Letharion Feb 15 '13 at 20:24
0

hook_menu does not get $node object as parameter. Node in 'node/%/revisions/%/offline' is just a path it is not any node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.