And*_*rew 3 drupal drupal-hooks drupal-modules
我有一个模块,声明了四种节点类型.我的问题是,hook_load,hook_view永远不会被调用.我使用drupal_set_message来查明是否正在调用某个钩子.我发现了hook_load,hook_view不是.只是为了给你清楚的图片,这是我的hook_load结构
这里有一个更新
function mymodule_node_info(){
return array(
'nodetype1' => array(
'name' => t('nodetype1'),
'module' => 'mymodule_nodetype1',
'description' => t('....'),
'has_title' => TRUE,
'title_label' => t('Title'),
'has_body' => TRUE,
'body_label' => t('Body'),
),
'nodetype2' => array(
......
'module' => 'mymodule_nodetype2',
......
),
'nodetype3' => array(
......
'module' => 'mymodule_nodetype3',
......
),
'nodetype4' => array(
......
'module' => 'mymodule_nodetype4',
.......
),
);
}
function mymodule_nodetype1_load($node){
$result = db_query('SELECT * from {nodetype1table} WHERE vid = %d'
$node->vid
);
drupal_set_message("hook_load is provoked.","status");
return db_fetch_object($result);
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么不叫它.我在drupal模块编写书上编写了这段代码,并按照说明进行操作.我已经尝试过该书中的示例代码,它可以正常工作.只有我的代码不起作用.可能是因为一个模块中有多个节点类型.任何帮助将受到高度赞赏.
小智 7
您的代码不会因为工作hook_load()
而hook_view()
不是模块钩子:他们是节点挂钩.调用基于内容类型名称,而不是模块名称.
因此,首先您需要使用hook_node_info()
以下方式声明您的内容类型:
function mymodule_node_info() {
$items = array();
$items['nodetype1'] = array(
'name' => t('Node Type 2'),
'module' => 'mymodule_nodetype1',
'description' => t("Nodetype 1 description"),
);
$items['nodetype2'] = array(
'name' => t('Node Type 2'),
'module' => 'mymodule_nodetype2',
'description' => t("Nodetype 2 description"),
);
$items['nodetype3'] = array(
'name' => t('Node Type 2'),
'module' => 'mymodule_nodetype3',
'description' => t("Nodetype 3 description"),
);
return $items;
}
Run Code Online (Sandbox Code Playgroud)
然后,您需要使用hook_node_info()
为为节点挂钩声明的每种内容类型指定的模块的名称.也就是说,mymodule_nodetype1_load()
,mymodule_nodetype2_view()
,等.
如果您在查看或加载节点时尝试触发基于非节点的模块,则需要使用hook_nodeapi()
:
function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'view':
mymodule_view_function($node);
break;
case 'load':
mymodule_load_function($node);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
Replace mymodule_load_function()
and mymodule_load_function()
with your own custom functions that are designed to act on the $node
object.
Besides the syntax error in your hook_load()
implementations, there's a piece of your code outside of what you're providing that's preventing the correct invocation. The following code works (if you create a nodetype1 node, the message "mymodule_nodetype1_load invoked" appears on the node): perhaps you can compare your entire code to see what you're missing.
function mymodule_node_info() {
return array(
'mymodule_nodetype1' => array(
'name' => t('nodetype1'),
'module' => 'mymodule_nodetype1',
'description' => t('....'),
'has_title' => TRUE,
'title_label' => t('Title'),
'has_body' => TRUE,
'body_label' => t('Body'),
),
'mymodule_nodetype2' => array(
'name' => t('nodetype2'),
'module' => 'mymodule_nodetype2',
'description' => t('....'),
'has_title' => TRUE,
'title_label' => t('Title'),
'has_body' => TRUE,
'body_label' => t('Body'),
),
);
}
function mymodule_nodetype1_form(&$node, $form_state) {
// nodetype1 form elements go here
return $form;
}
function mymodule_nodetype2_form(&$node, $form_state) {
// nodetype2 form elements go here
return $form;
}
function mymodule_nodetype1_load($node) {
$additions = new stdClass();
drupal_set_message('mymodule_nodetype1_load invoked');
return $additions;
}
function mymodule_nodetype2_load($node) {
$additions = new stdClass();
drupal_set_message('mymodule_nodetype2_load invoked');
return $additions;
}
Run Code Online (Sandbox Code Playgroud)
If you're not reseting your environment after changes to your module, you might be running into caching issues. You should test your code in a sandbox environment that can be reset to a clean Drupal installation to ensure you're not focusing on old cruft from previous, incorrect node implementations.
Additionally, you should only be using hook_nodeapi()
if you are trying to act on content types that are not defined by your module. Your content types should be using the node hooks (hook_load()
, hook_view()
, etc.).
Finally, it may be the case that you're using the wrong hooks because you're expecting them to fire in places they are not designed to. If you've gone through everything above, please update your post with the functionality you're expecting to achieve and where you expect the hook to fire.
归档时间: |
|
查看次数: |
2844 次 |
最近记录: |