6

I learned yesterday that the correct way to create a node type is by implementing hook_node_info inside a .module file. Before I had been programatically creating node types in a .install file via node_type_save, like this:

// Create node type
$type = array(
  'type' => 'fruits',
  'name' => st('Fruits'),
  'base' => 'node_content',
  'description' => 'Contains information about fruits.',
  'custom' => 1,
  'modified' => 1,
  'locked' => 0,
);
$type = node_type_set_defaults($type);
node_type_save($type);

I had also been setting the node type options in the .install file, like this:

// Set 'fruit' options.
variable_set('node_preview_fruit', 0);                     // Set 'Preview before submitting' to 'Disabled'.
variable_set('node_options_fruit', array('status'));       // Set 'Default options' to 'Published'.
variable_set('node_submitted_fruit', 0);                   // Unset 'Display author and date information'.
variable_set('comment_fruit', COMMENT_NODE_CLOSED);        // Set 'Default comment setting for new content' to 'Closed'.
variable_set('menu_options_fruit', array('navigation'));   // Set 'Available menus' to 'Navigation'.
variable_set('menu_parent_fruit', 'navigation:0');         // Set 'Default parent item' to '<Navigation>'.

Is this the correct way to programatically set node type options: like this, inside the .install file, even though the node type itself is created inside the .module file?

2 Answers 2

2
+50

Looking at the code for node_type_form_submit you can see that the variables are set before the node_type_save() function is called. Thinking about it a bit, it doesn't really matter as the node type info is saved into a different table than variables. So as long as the variables are being set synchronously with the creation of the node type, it shouldn't matter. Even then, the worst case scenario would be you'll have some redundant variables in the variables table. Which is not a big issue.

So to answer the question, yes that's the proper way. However I'd do it in the same function call in the .install file, maybe implement it in hook_install or hook_enable depending on what you need.

2
  • Okay, I'll stick with this approach then.
    – dbj44
    Commented Jul 22, 2013 at 20:23
  • Me too. Until someone else tells different. Additional info to that issue on drupal.org/node/1169864
    – leymannx
    Commented Mar 11, 2015 at 13:59
0

if you wanted to know the best way to create node on enabling the Drupal module

  1. create a feature containing content type you want to create on module enable

  2. check the code it generate

    -> hook_field_info is defined in separate inc file and include it in .module by require once

as feature follows all the best drupal technique to code so i think this is the best way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.