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)

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

9 responses to “Disable upload and comment for a new content type programmatically”

  1. 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.

    • 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.

  2. 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

  3. 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);

  4. 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.

%d bloggers like this: