views:

37

answers:

2

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.

+2  A: 

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.

Mark Trapp
As I'm still learning drupal, I'm not sure what you mean by non-node based module. If you are talking about module that doesn't have custom node implementation, then mine is node based module. Because my module is all about implementing custom nodes and forms for those nodes. As for your sample code, hook_nodeapi method works like a charm. But, one question though - my test message output from drupal_set_message is outputted three times. So, is it common that hook_nodeapi is called three times in a row?As for hook_load code, however, it doesn't work at all.
Andrew
I've changed module array element value inside hook_node_info and declaration of hook_load function, still it is not called. Any idea what the problem might be. And thanks very much for your help. I really appreciate it.
Andrew
Node-based modules are ones that declare custom content types using `hook_node_info()`. As for your issues, please edit your original post and add the code for your `hook_load()` and `hook_node_info()` implementations.
Mark Trapp
I've edited. Hook_load is not called at all.
Andrew
I've redacted the rest of the node type structures and replaced them with "....." just to make easy to read.
Andrew
Updated my answer.
Mark Trapp
Hello Mark, posted my response.
Andrew
A: 

Hello Mark,
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.

Andrew
Ah yeah, I figured it was something like a missing `node_form()` or something, but figured I'd throw out everything I'd try rather than guess and check. Glad to see you got it fixed.
Mark Trapp

related questions