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)

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)