Disable upload and comment for a new content type programmatically

Following code is useful when installing a module that create a new content type programmatically on Drupal 6.x.

Basically, it adds two variables setting default values for comments (core Comment module) and attachments (core Upload module).

Code to write on my_funny_module/my_funny_module.install.

function my_funny_module_install() {
  // Disable attachments
  // Read http://api.drupal.org/api/function/upload_nodeapi/6 on "load"
  variable_set("upload_my_content_type", 0);

  // Disable comments for this content type
  // Read http://api.drupal.org/api/function/comment_form_alter/6
  variable_set('comment_my_content_type', COMMENT_NODE_DISABLED);

  // Install schema as usual (if any)
  drupal_install_schema('my_funny_module');
}

Note that this code assign only default values for my_content_type: as any content type, this value could be later changed via GUI.

Advertisements

9 thoughts on “Disable upload and comment for a new content type programmatically

  1. chirale Post author

    These lines set only default values for your content type before it is programmatically created (on my_funny_module.install). If you’ve already created this content type, you have to change it via GUI on admin/content/node-type/my-funny-modules > Workflow settings.

    Reply
    1. chirale Post author

      Note that both methods (via GUI or via mymodule.install) apply only to new created content, not for old one (for old nodes you have to change comment settings for any node manually). I don’t know any other way to set comment settings values apart these two, and both apply only to contents created after this change. I hope that this help.

      Reply
  2. Gareth

    You could write an update script.

    function my_funny_module_update_1() {
    // Disable comments for this existing nodes of this content type
    $ret[] = update_sql(“UPDATE {node} SET comment = 0 WHERE type = my_content_type”);

    return $ret
    }

    Reply
  3. Gareth

    Also your variable_set function calls should also tell which variable it is setting e.g.
    variable_set(“upload_my_content_type”, 0);
    variable_set(‘comment_my_content_type’, COMMENT_NODE_DISABLED);

    Reply
  4. don

    Thanks for this! I just want to add that if you are working on Drupal 7, the constant to use is COMMENT_NODE_CLOSED instead of COMMENT_NODE_DISABLED.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s