When creating a node type programatically, some node type options can be set in hook_node_info, including name, base, description, has_title, title_label, has_body, and help. As far as these node options go, using hook_node_info is cleary the canonical way of doing it.

However, some additional node type options cannot be set using this hook, and I have not yet found any canonical documentation that explains how do do this.

The options I look for a method of setting programatically include:

  • whether commenting by default are open or closed;
  • whether the node should be published, promoted, sticky and/or have revisions enabled upon creation;
  • whether it should display author and date information.

The solution I currently use is to set the appropriate database variables in hook_install for the module that creates my_nodetype to the defaults I want, e.g.:

variable_set('comment_my_nodetype', COMMENT_NODE_CLOSED);
variable_set('node_options_my_nodetype', array('status','promote'));
variable_set('node_submitted_my_nodetype', FALSE);

I figured out this solution by reading the source code to discover that these database variables determine behavior at runtime. I've not found documented anywhere how one should go about setting these additional node type options.

Is there a better way of doing this than the improvised soluton I currently use?


Is there a better way of doing this than the improvised soluton I currently use?

I don't think so. Also I don't find it improvised.

Here's a D7 module I once made that creates a new content type: https://github.com/leymannx/drupal-personality-test/blob/master/personality_test.install

 * Implements hook_install().
function MYMODULE_install() {
  $types = node_type_get_types();

  // Add a body field

  // Add custom fields

  // Default to not to be promoted
  variable_set('node_options_MY_CONTENTTYPE', array('status'));

  // Disable comments
  variable_set('comment_MY_CONTENTTYPE', COMMENT_NODE_CLOSED);

  // Hide date and author information
  variable_set('node_submitted_MY_CONTENTTYPE', FALSE);

  // Disable node preview
  variable_set('node_preview_MY_CONTENTTYPE', 0);

  // Disable menu entry
  variable_set('menu_options_MY_CONTENTTYPE', array(0));

I experienced just the same and solved it exactly like you via hook_install. If I remember it right, I probably found the code in core when looking how the article and page node types get created.

Related: How to set node type options programmatically (where another tiny module gets recommended you'd need to have everything together in hook_node_info).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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