I have a hook menu
$items['node/%/delete'] = array(
'title' => 'Delete',
'load arguments' => array(3),
'description' => 'Confirm the action.',
'page callback' => 'drupal_get_form',
'page arguments' => array('_mymodule_delete', 1),
'type' => MENU_CALLBACK,
'weight' => -4,
);
return $items;
What do I need to do to make sure the following function gets to work (and the variables get their values):
function _mymodule_delete ($form, $form_state, $node) {
...
}
In orther words, how do I get values in the three arguments given ( $form
, $form_state
and $node
)
EDIT
I'll have another go...
The only argument(s) you need to pass to drupal_get_form
, other than the name of the form itself, are arguments specific to that form function; $form
and $form_state
are automatically added for you. So if you want to call a form with a signature of _mymodule_delete($form, $form_state, $node)
you would use this code:
$form = drupal_get_form('_mymodule_delete', $the_node);
When you apply this to the menu router, all you're looking to do is pass the loaded $node
through as an argument to drupal_get_form
in the same way. Your router item would look like this:
$items['node/%node/delete'] = array(
'title' => 'Title',
'page callback' => 'drupal_get_form',
'page arguments' => array('_mymodule_delete', 1),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
'weight' => -4,
);
Your original example is missing the access arguments
/ access callback
attribute which would make your page inaccessible (403 status) so I've added in the 'standard' access arguments of access content
. You'll probably want to change this for your own needs.
The string node/%node/delete
(the router path) and the page arguments array are the bits you're probably interested in here. When you want to pass an argument from the URL to a callback function you simply include it's 'index' as one of the page arguments. This index comes from a zero-based array of the router path when split by the separator ( /
).
In this example the three parts of the path are:
0 => 'node',
1 => '%node',
2 => 'delete'
As the variable element of the path is in index position 1
, that's the number we pass to the page arguments
array.
Just to make it a little more complicated, the variable passed in through the path can also be passed to a load function before it's passed to the page callback
function. For some reason the naming convention in Drupal is that a function with the name of the variable with _load
appended to it will be the name of the function called.
So in this case, node_load
is called. If your router path was, for example, books/%book
then a function called book_load
would be called to prepare the variable to be passed to the page callback
function.
The load function is optional, if your path was node/%/delete
then the argument passed to your form callback would be the exact string (in this case a node ID) from the URL.
I'm sure you've seen it but the hook_menu()
documentation tries its best to explain all this, I can understand why it would be difficult to comprehend though.
Hope that helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.