dqb78642
2012-01-10 16:03
浏览 68
已采纳

hook_menu问题

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)

图片转代码服务由CSDN问答提供 功能建议

我有一个钩子菜单

  $ items ['node  /%/ delete'] = array(
'title'=>'删除',
'加载参数'=> array(3),
'说明'=>'确认操作。',  
'page callback'=>'drupal_get_form',
'page arguments'=> array('_ mymodule_delete',1),
'type'=> MENU_CALLBACK,
'weight'=>  -  4,
); 
return $ items; 
   
 
 

我需要做些什么来确保以下函数能够正常工作(变量得到它们的值 ):

function _mymodule_delete($ form,$ form_state,$ node){
...
}

或者说,如何在给出的三个参数中获取值( $ form $ form_state $ node

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzouhe9734 2012-01-10 16:12
    已采纳

    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.

    点赞 打赏 评论

相关推荐 更多相似问题