By babipanghang on
I'm trying to create a module which should create a new content-type. I've managed to actually add a node-type, but there are no fields but the default ones, and I figured out why: my hook_form never gets executed.
My test-code:
// hook_disable() implementation
function contenttypetest_disable(){
error_log('hook_disable called!');
node_type_delete('contenttypetest_node');
}
// hook_uninstall() implementation
function contenttypetest_uninstall(){
error_log('hook_uninstall called!');
node_type_delete('contenttypetest_node');
}
// hook_form implementation
function contenttypetest_form(&$node, $form_state){
error_log('hook_form called!');
$type = node_type_get_type($node);
$form = array();
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#default_value' => !empty($node->title) ? $node->title : '',
'#weight' => -5,
);
$form['contenttypetest_pass'] = array(
'#type' => 'password',
'#title' => t('Type a password'),
'#description' => t('You can type anything you like.'),
);
$form['contenttypetest_veld'] = array(
'#type' => 'file',
'#description' => 'You might wanna upload a file!',
'#title' => 'Bestand',
);
return $form;
}
// hook_node_info() implementation
function contenttypetest_node_info() {
error_log('hook_node_info() called.');
return array(
'contenttypetest_node' => array(
'name' => t('Content type test node'),
'module' => 'contenttypetest',
'description' => t("This nodetype is a test how to create nodetypes."),
),
);
}
I enabled the module, gone to add content, and clicked the contenttypetest content type, just to be sure there's a moment hook_form needs to be executed. Then, i disabled the module. That generates the following log:
[25-Mar-2011 10:36:57] hook_node_info() called.
[25-Mar-2011 10:36:57] hook_node_info() called.
[25-Mar-2011 10:37:01] hook_node_info() called.
[25-Mar-2011 10:37:39] hook_disable called!
After long searching, I'd really appreciate some help.
Thanks in advance,
Jaap
Comments
I am not sure why your
I am not sure why your hook_form() is not getting called, but a few other notes:
hook_uninstall() should be in your .install file, not in .module.
Your arguments for hook_form() are for Drupal 6, but your post is tagged with Drupal 7. Check http://api.drupal.org/api/drupal/modules--node--node.api.php/function/ho....
---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com
Sharp catch
Sharp catch about hook_form. I used the drupal 6 example code as a basis, since no hook_form() example exists for drupal 7.
Unfortunately, correcting that doesn't seem to solve my problem (which was expected - after all, there IS a hook_form() defined which takes 2 arguments, and that should be enough for PHP).
Regarding moving hook_uninstall() to the .install file; that didn't seem to make any difference at all. I guess that's just a matter of tidyness anyway.
Thanks for your help!
Did i answer your question on the forums? I love to hear a reply wether or not it worked for you!
Jaap - Acquia certified drupal site builder
same
Hello bbp,
I have the same thing here, must be missing something obvious.
How did you solve it?
Naming conventions
OK, it seems you have to use the correct names in hook_info. The only gets called if your contenttaype array is named the same as your module.
Wrong content type array
Hello,
I had the same issue and i found where was the problem.
In the implementation of hook_node_info(), the return value has to be an array of sub-arrays describing the content types that we want to create trough our module.
The key of a sub-array has to be the machine readable name of the content type, and in this sub-array, the value of the key 'base' has to be the name of the module implementing the content type. This value is used to construct the callbacks hook_insert, etc...
See http://api.drupal.org/api/drupal/modules--node--node.api.php/function/ho...
I hope that my post helps you.
Fab.
Hi:I had a same
Hi:
I had a same problem.converted a drupal 6 module to drupal 7, and the hook_form never get called.
I have found out that in drupal 7, the node/add/$type menu path was called by
the node_add function which will call "drupal_prepare_form" function.
the "drupal_prepare_form" now added following code which was not in drupal 6 version.
the above code will supply a "#theme" to the form, in your case i think is "contenttypetest_node_form" which is a form id ( look at node_add function to see how this form id generated).
usually , if we create a customized module , we create a node type. since every thing
in drupal 7 are node, except Users, blocks, and comments. and a node type has a default '#theme',
node_form.
In you form, there is not a theme function called "contenttypetest_node_form". that why hook_form failed.
there are 2 solutions to this problem.
1> create a hook function ,
above code will remove contenttypetest_node_form from '#theme' array, so the drupal 7 theme system will not looking for 'contenttypetest_node_form', but looking for default '#theme' - node_form.
2> register 'contenttypetest_node_form' in hook_theme.
Also , you have to replace 'module' with 'base' in 'contenttypetest_node_info' function.
module vs base
I had the exact same problem when upgrading a d6 module to d7. ctx2002 mentioned making sure to change 'module' to 'base' when defining your content type in hook_node_info, and that was all that was required for my hook_form to begin being called. I did have to clear all caches first, and then it worked like a charm. Thanks!