I'm creating a node type programmatically during my module install and wish to set certain node type options. I've done a lot of searches, as well as dumping variables when saving the node type, to determine how these settings are saved. So far, the only one I've found that works concerns comments. Below is the code I'm using to create the node type.

// Turn off comments
variable_set('comment_bir_news', '1');
  
$t = get_t();
// Define the node type.
$bir_news = array(
  'type' => 'bir_news',
  'name' => $t('BIR News'),
  'base' => 'node_content',
  'description' => $t('News items for BIR.'),
  'body_label' => $t('Text')
);
$content_type = node_type_set_defaults($bir_news);
node_add_body_field($content_type);
node_type_save($content_type);

Below are the settings and values I wish to set:
Submission form settings: Preview before submitting - Disabled
Publishing options: Promoted to front page - unchecked
Display options: Display author and date information - unchecked
Menu settings: Just need to know how to set these. (e.g. choosing User menu and default parent)

Would appreciate if someone could show me how these are added to the array above while saving, set variables, or whatever else needs to be done while saving the node type.

Thanks

Comments

rbruhn’s picture

Never mind. Decided to just see what was set in the variables table after modifying the node type form. Came up with the below and it seems to work.

variable_set('additional_settings__active_tab_bir_news', 'edit-menu');
variable_set('comment_bir_news', '1');
variable_set('menu_options_bir_news', array('user-menu'));
variable_set('menu_parent_bir_news', array('user-menu:0'));
variable_set('node_options_bir_news', array('status'));
variable_set('node_preview_bir_news', 0);
variable_set('node_submitted_bir_news', 0);
mtrolle’s picture

I'm working on the exact same detail.
There must be a smarter way to handle this in the Drupal API but I can't find anything.

If this is the way to do it... Wauw. I really don't understand the hype about Drupal.
The API is so badly documented and only way to understand Drupal is to look around in the core system all the time...

jg314’s picture

Did you find any better way to handle this then to edit the variables table?

Anonymous’s picture

There is no better way, additional values that are added to the node type form are saved as variables in the form submit function itself. The only way around it would be to implement your own version of the submission function and attach it to the form using a form_alter. In your custom submission function you could then save the extra data to a custom table. I wouldn't recommend this though, it would be far better to stick to the way that the submit function already handles it.

Have a look at the node_type_form's submit handler and it should make perfect sense to anyone: http://api.drupal.org/api/drupal/modules--node--content_types.inc/functi...

peterx’s picture

I ended up writing a module, Content type, to read a content type definition from a .info style text definition so I do not have to go through the hassle each time I set up a module. Just type in the details and it is loaded with the module. Would be nice to have something like it for D8.

stldrupal’s picture

Thanks for posting this! It saved me a significant amount of time!

mayankkandpal’s picture

sub

Anonymous’s picture

All the additional settings are provided by hook_form_FORM_ID_alter in their respective modules (eg menu_form_node_type_form_alter). When the node type form is saved each of these additional settings are saved using the pattern 'formkey_contenttype' in the variables table (See line 317 onwards of /modules/node/content_types.inc).

I guess the most structured way to do this would be to build up a form_state array as expected by the node_type_form_submit function and call drupal_execute on the form itself.

mtrolle’s picture

That seems like an extremely "hacky" solution.
But of course it would be a more secure solution than setting the variables directly yourself, since the core modules can be changed in behavior.

Anonymous’s picture

I wouldn't consider that 'hacky' at all, you would simply be submitting a particular form to take advantage of it's submission workflow. That's why the FAPI and functions like drupal_execute exist. If you go to the extreme lengths of providing hooks and database storage for every tiny bit of the system then it would become extremely bloated and slow. That's why some of the behaviour that will not need to be changed by 95% of users is handled in the most efficient way possible, but not necessarily in keeping with the way other parts of the system behave.

I understand your frustration with the learning curve involved with Drupal, it took me a long time to become totally happy with it too but with the best will in the world, can you name a better open source PHP framework with as much contributed code and community support? There is no such thing as a system that will do everything for everybody, but out of the many, many different OS frameworks in many different languages I've tried down the years, Drupal stands heads and shoulders above the rest. In my opinion only the .NET framework beats it for ease of programming.

I'm not having a go, the attitude you have towards Drupal is exactly the attitude I had towards it a year or so ago...my advice is to persevere, the benefits far outweigh the problems when you know the system well!

Stutzer’s picture

I've also ran up with this inconvenience yesterday and wrote a tiny module (for D7) aimed to solve it.
It extends hook_node_info() in the following way:

function hook_node_info() {
  return array(
    'customtype' => array(
      'name' => t('Custom node type'),
      'base' => 'custom',
      ...

      /**
       *  Submission form settings (core functional)
       *  ------------------------------------------
       *
       *  Preview before submitting
       *    node_preview_{$type}
       *    value <int>:
       *      0 => Disabled
       *      1 => Optional
       *      2 => Required
       */
      'node-preview' => 0,

      /**
       *  Publishing options (core functional)
       *  ------------------------------------
       *
       *  Default options
       *    node_options_{$type}
       *    value <array>:
       *      array('status', 'promote', 'sticky', 'revision')
       */
      'node-options' => array('status'),

      /**
       *  Display settings
       *  ----------------
       *
       *  Display author and date information
       *    node_submitted_{$type}
       *    value <int>:
       *      0 => Hide author and date information
       *      1 => Display author and date information
       */
      'node-submitted' => 0,

      /**
       *  Comment settings
       *  ----------------
       */
      'comment' => array(
        /**
         *  Default comment setting for new content
         *    comment_{$type}
         *    values <int>:
         *      0 => hidden
         *      1 => closed
         *      2 => open
         */
        'status' => 2,

        /**
         *  Threading
         *    comment_default_mode_{$type}
         *    values <int>:
         *      0 => don't show
         *      1 => show
         */
        'default-mode' => 1,

        /**
         *  Allow anonimous to leave commentns
         * 	  comment_anonymous_{$type}
         *    values <int>:
         *      0 => forbid
         *      1 => allow
         */
        'anonymous' => 1,

        /**
         *  Comments per page
         *    comment_default_per_page_{$type}
         *    values <int>:
         *      10, 30, 50, 70, 90, 150, 200, 250, 300
         */
        'default-per-page' => 50,

        /**
         *  Show reply form on the same page as comments
         *    comment_form_location_{$type}
         *    values:
         *      0 => hide
         *      1 => show
         */
        'form-location' => 1,

        /**
         *  Preview comment
         *    comment_preview_{$type}
         *    values <int>:
         *      0 => disabled
         *      1 => optional
         *      2 => required
         */
        'preview' => 0,

        /**
         *  Allow comment title
         *    comment_subject_field_{$type}
         *    values <int>:
         *      0 => disallow
         *      1 => allow
         */
        'subject-field' => 0,
      ),
    ),
  );
}

Module isn't yet tested carefully enough but it works good and available here:
https://github.com/antipin/drupal.nodetools

Navneet.Chauhan’s picture

I used this:

$node = array(
    'type' => NODE_NAME,
    'name' => $t('Event Calendar'),
    'base' => 'node_content',
    'locked' => TRUE,
    'node-preview' => 0,
    'node-options' => array('status'),
    'comment' => array('status' => 1),
    'description' => $t('A content type to create events for calendar.'),
    'title_label' => $t('Event Title'),
    'custom' => FALSE
  );
  $content_type = node_type_set_defaults($node);
  //add body field using drupal function
  node_add_body_field($content_type, $t('Event Description'));
  node_type_save($content_type);

Still after it, comments are open, node-preview is enable and promote option is also enabled. what is wrong with my code ? Help

baohanddd’s picture

Those settings are extra, you have to enable below module first and these settings can works.
https://github.com/antipin/drupal.nodetools/blob/master/nodetools.module

talreg’s picture

The table for the set and get variable is simply called variable under dp7.