hook_load/hook_view 未调用

发布于 2024-09-16 13:19:26 字数 1293 浏览 3 评论 0原文

我有一个声明了四种节点类型的模块。我的问题是,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);
}

我不知道为什么它没有被调用。我根据 drupal 模块编写书编写了此代码并按照说明进行操作。我已经尝试过那本书中的示例代码,它工作正常。只有我的代码不起作用。可能是因为一个模块中有多种节点类型。任何帮助将不胜感激。

I have a module with four node types declared. My problem is, hook_load, hook_view is never called. I used drupal_set_message to find out if certain hook is being called. And I found out hook_load, hook_view isn't. Just to give you clear picture, here's my structure of hook_load

HERE'S UPDATED ONE

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);
}

I don't know why it is not called. I wrote this code base on drupal module writing book and follow the instructions. I've tried sample code from that book and it works ok. Only my code isn't working. Probably because of multiple node types in one module. Any help would be highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不知所踪 2024-09-23 13:19:26

您的代码不起作用,因为 hook_load() 和 < a href="http://api.drupal.org/api/function/hook_view" rel="noreferrer">hook_view() 不是模块钩子:它们是节点钩子。调用基于内容类型名称,而不是模块名称。

因此,首先您需要使用 hook_node_info()< 声明您的内容类型/a>:

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;
}

然后,您需要为节点挂钩使用为 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;
  }
}

替换 mymodule_load_function()mymodule_load_function() 以及您自己的自定义函数,这些函数旨在作用于 $node 对象。


编辑2

除了 hook_load() 实现中的语法错误之外,您所提供的代码之外还有一段代码阻止了正确的调用。以下代码有效(如果您创建一个 nodetype1 节点,消息“mymodule_nodetype1_load invoked”会出现在该节点上):也许您可以比较整个代码以查看缺少的内容。

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;
}

如果您在更改模块后没有重置环境,则可能会遇到缓存问题。您应该在沙盒环境中测试您的代码,该环境可以重置为干净的 Drupal 安装,以确保您不会关注以前错误的节点实现带来的旧问题。

此外,如果您尝试对模块未定义的内容类型执行操作,则应仅使用 hook_nodeapi()。您的内容类型应该使用节点挂钩(hook_load()hook_view() 等)。

最后,您可能使用了错误的钩子,因为您希望它们在非设计的地方触发。如果您已完成上述所有操作,请使用您期望实现的功能以及您期望触发钩子的位置来更新您的帖子。

Your code doesn't work because hook_load() and hook_view() aren't module hooks: they're node hooks. The invocation is based off of content type names, not module names.

So, first you need to have declared your content types using 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;
}

Then, you need to use the name of the module you specified for each content type declared in hook_node_info() for your node hooks. That is, mymodule_nodetype1_load(), mymodule_nodetype2_view(), etc.


Edit

If you're trying to have a non-node based module fire when a node is viewed or loaded, you need to use 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;
  }
}

Replace mymodule_load_function() and mymodule_load_function() with your own custom functions that are designed to act on the $node object.


Edit 2

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;
}

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.

许久 2024-09-23 13:19:26

我找到了你的代码不起作用的罪魁祸首。这是因为我使用的是旧代码创建的测试数据。在我的旧代码中,由于 hook_node_info 中的节点声明使用相同的模块值,我只能创建一个 hook_form 实现并使用“switch”语句返回适当的形式。只是为了让您清楚地了解我的旧代码 -

function mymodule_node_info(){
   return array(
      'nodetype1' => array(
         .....
         'module' => 'mymodule',
         .....
     ),
     'nodetype2' => array(
         ......
         'module' => 'mymodule',
         ......
     ),
         .......

    );

 }
function mymodule_form(&$node, $form_state){
switch($node->type){
    case 'nodetype1':
        return nodetype1_form();
    break;      

    case 'nodetype2':
        return nodetype2_form();
    break;
    .....

}
}

当我在进行您提供的更改后创建新数据时,会调用 hook_load 。有用!我已经测试了几次(使用以前的代码创建的旧数据进行测试,并使用这些更改后创建的新数据进行测试)以确保这是否是根本原因,并且我得到了相同的结果。我认为 drupal 存储 form_id 或模块条目值节点声明和数据并确定 hook_load 调用。这可能就是为什么它不认为它是该节点的数据,因此不调用 hook_load 的原因。

非常感谢您的帮助。

I found the culprit why your code doesn't work. It's because I was using the test data created by the old codes. In my old codes, because of node declaration inside hook_node_info uses the same module value, I could only create one hook_form implementation and use "switch" statement to return appropriate form. Just to give you clear picture of my old codes-

function mymodule_node_info(){
   return array(
      'nodetype1' => array(
         .....
         'module' => 'mymodule',
         .....
     ),
     'nodetype2' => array(
         ......
         'module' => 'mymodule',
         ......
     ),
         .......

    );

 }
function mymodule_form(&$node, $form_state){
switch($node->type){
    case 'nodetype1':
        return nodetype1_form();
    break;      

    case 'nodetype2':
        return nodetype2_form();
    break;
    .....

}
}

When I created new data after I made those changes you have provided, hook_load is called. It works! I've tested several times(testing with old data created by previous code and testing with new data created after those changes) to make sure if that's the root cause and, I got the same result.I think drupal store form_id or module entry value of node declaration along with data and determine the hook_load call. That's probably the reason why it doesn't think it's a data of this node and thus hook_load isn't invoked.

And Thank you so much for your help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文