I am building a website and have 3 content-types.

Type A
Type B
Type C

I built these content types one after another, and used existing field when creating the content-type field Field.

My question is, how do I set a default value for Field for each of my content-types ? Right now when I set a default it is default for all my content-types.

I want to have

Type A -> Field = value 1
Type B -> Field = value 2
Type C -> Field = value 3
  • i think you should use distinct fields for that if you do not want to set a default value programmaticly
    – pico34
    Sep 16, 2013 at 14:52
  • so, this cannot be done unless i do some custom code, right ? I'm asking because this is the situation right now, on a project that I'm continuing, I did not develop it from scratch, I just have to improve it and add some custom functionalities. Sep 16, 2013 at 14:55
  • If you mean there is already custom code doing it, that is a perfectly acceptable solution. It would only require a very small amount of custom code.
    – rooby
    Sep 16, 2013 at 15:00

2 Answers 2


You can easily achieve you goal with the rules module, as @pico34 wrote in his answer, but use the trigger Before saving content instead of After saving new content.

Alternativey, if you need to do that programmatically, you can define your default values in just a few lines of code using hook_node_presave()


 * Implements hook_node_presave().
function YOUR_MODULE_node_presave($node) {
  // set the value of field "field_somefield" in case of node type: a, b or c.
  if (in_array($node->type, array('a', 'b', 'c')) && empty($node->field_somefield[$node->language][0]['value'])) {
    switch ($node->type) {
      case 'a' :
        $node->field_somefield[$node->language][0]['value'] = 'Default value for A';
      case 'b' :
        $node->field_somefield[$node->language][0]['value'] = 'Default value for B';
      case 'c' :
        $node->field_somefield[$node->language][0]['value'] = 'Default value for C';

You can use Rules module

For each content type, add a rule :

  • The event trigger is : after saving new content

  • Add a distinct condition on each: content is type 'foo'

  • Add another condition : 'data value is empty' on your field

  • Add an action : "Set a data value" and select your field and add your default value

OR pre-populate the field in the form

function custom_form_alter(&$form, &$form_state, $form_id) {

    switch ($form_id) {

      case 'typea_node_form':
        if (!$form['nid']['#value']) {// = add only
          $form['field_custom']['und'][0]['#default_value']['value'] = 'default_a';

      case 'typeb_node_form':
      // the same...

  • Wouldn't that overwrite any value the user enters? Don't you need a condition to say "if the field is empty"? However then in that case the user could never make the field empty like they can by removing the default value normally, which may or may not be an issue depending on whether the field is required or not.
    – rooby
    Sep 16, 2013 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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