3

I have a D7 module, which defines a content type, via the appropriate hooks.

By default, comments are turned on for my content type. How do I set the default to off, inside of the code of my module? (NOT through the Drupal interface)

Thanks in advance.

1

These instructions will enable you to identify and set any variable you'd like in Drupal 7. Please feel free to ask for clarification.

I'd start by installing the Devel module so you can see the variable you need to set. This will allow you to inspect the array where this setting is defined.

Visit a node of the content type you're working with. You'll see a tab called "Devel". Follow it and you'll be able to see the array which defines a node of this content type. There should be a value called "Comment", and I believe it needs to be set to "1" to disable comments.

As a sidenote, the Features Overrides module does this by setting $overrides["variable.comment_mycontenttype.value"] = '1';

But, since we're not using that module, we'll set it manually.

You should be able to define that variable inside your module code by using the variable_set() function.

That code can live inside hook_update_N(), which is executed when update.php is run. Simply add the code for your update hook inside your custom module and run update.php on your site.

0

The comment module defines some constants we can use for custom content types. In your module's MODULE_NAME.install file, use the following (assumes content type is 'example'):

/**
 * Implements hook_install().
 */
function MODULE_NAME_install() {
  // Do not promote "Example" nodes to the front page by default.
  variable_set('node_options_example', array('status'));

  // Close comments for all "Example" nodes by default.
  variable_set('comment_example', COMMENT_NODE_CLOSED);

  // Do not display submitted (date and author) for all "Example" nodes.
  variable_set('node_submitted_example', FALSE);
}

Make sure this comes after you build the fields and field instances. Included additional variables you can change provided by the node module.

Another option is in hook_node_info():

/**
 * Implements hook_node_info().
 */
function MODULE_NAME_node_info() {
  $items = array(
    'example' => array(
      'name' => t('Example'),
      'base' => 'node_content',
      'description' => t('Use <em>example</em> to create example nodes.'),
      'has_title' => '1',
      'title_label' => t('Title'),

      // Do not promote "Example" nodes to the front page by default.
      'node-options' => array('status'),

      // Close comments for all "Example" nodes by default.
      'comment' => array(
        'status' => 0,
      ),

      // Do not display submitted (date and author) for all "Example" nodes.
      'node-submitted' => 0,
    ),
  );
  drupal_alter('node_info', $items);
  return $items;
}

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.