I’ve been working with Drupal for about 3 weeks now so I’m not incredibly familiar with but enough to develop some simple modules. Anyways my module has 3 custom content types defined with a separate insert hook and they all just stopped working. When the form is submitted for each one the data is saved to the nodes table but the insert_hook seems to never fire. This has me stumped as I’ve tried removing parts of code but nothing. The hook_insert never seems to fire. I can’t say for certain but it seems like this problem arose after hook_load was implemented. I don’t understand how that would cause the hook_insert not to fire. I even tried removing that hook_lood code but still the hook_insert just isn’t firing. Anyone have any ideas? I’ve looked on google and I can’t seem to find anything similar not related to contributed modules such as cck. This issue has me a little stumped.
The module itself is professional work so I’m not really sure if I feel right posting the the entire thing up here. The problem seems to be implementing hook_load entirely annihilated the form submission for some reason. Forms in the module build properly but no data is getting submitting I think. I’m trying to track the issue and see when it occurs but the thing is Drupal is just so disconnected its difficult to know when thing begins and another starts. This is really driving me mad. Now I remember why I hate open source frameworks and cms software. Drupal just seems to be a uncommented utter mess make it difficult to figure this out.
Resolved the issue. The lesson here is to remember that nodes passed to hook_insert have already been placed in the database. Therefore, you can’t use the existence of an nid to switch between updating or inserting because it will always exist.
Problem code:
/*
* Save chapter data to db
*/
function modname_insert($node) {
/*
* This will ALWAYS happen
*/
if($node->nid) {
return db_query(
'UPDATE {modname_chapter} books_id = %d, chapter_number = %d WHERE nid = %d'
,$node->books_id
,$node->chapter_number
,$node->nid
);
} else {
return db_query(
'INSERT INTO {modname_chapter} (nid,books_id,chapter_number) VALUES (%d,%d,%d)'
,$node->nid
,$node->books_id
,$node->chapter_number
);
}
}
Resolution Code:
/*
* Save chapter data to db
*/
function modname_insert($node,$new=true) {
if(!$new) {
return db_query(
'UPDATE {modname_chapter} books_id = %d, chapter_number = %d WHERE nid = %d'
,$node->books_id
,$node->chapter_number
,$node->nid
);
} else {
return db_query(
'INSERT INTO {modname_chapter} (nid,books_id,chapter_number) VALUES (%d,%d,%d)'
,$node->nid
,$node->books_id
,$node->chapter_number
);
}
}
A duplicate key update could also get around the issue. Either way its fixed.
Hi Oddz if you can provide your module code or a file attachment i could help you. You can also refer to this url: http://api.drupal.org/api/drupal/developer--examples--node_example--node_example.module/6