I just created a new content type and would like to have the body filled with a default value. How do you do that??

Comments

razevedo’s picture

Hi, have you found a way to do this?

broon’s picture

Hi there,

using template functions and form api this may be achieved. First locate your appropiate template.php within your current theme folder. Add a new function:

/**
* Theme override for node edit form.
*
* The function is named themename_formid.
*/
function YOURTHEMENAME_node_form($form) {
  // Initialize output variable
  $output = '';
  // Print out the $form array to see all the elements we are working with.
  // Note: This is for the developing phase only. Disable it once you found
  // the variables you want to work on. Devel module required.
  #$output .= dsm($form);
  // Once we know which part of the array we are after we can change it.
  // First though disable the line with dsm($form) in it.

  // insert INTERESTING PART here

  // Make sure we call drupal_render() on the entire $form to make sure
  // we still output all of the elements (particularly hidden ones needed
  // for the form to function properly.)
  $form['body_field']['#suffix'] .= "<script type='text/javascript'>var bodybox = $('#edit-body'); bodybox[0].innerHTML = 'This is the default text for body when creating new nodes.';</script>";
  $output .= drupal_render($form);
  return $output;
}

This piece of code by itself does nothing yet, so let's take a look at the INTERESTING PART. If you've ever worked with form API before you might think of trying something like this:

  $form['body_field']['body']['#default_value'] = 'This is the default text for body when creating new nodes.';

However, it doesn't work, probably because of the javascript functions running on #after_build.
But we can use javascript, too, to populate the body field. We're just using the #suffix of the bodyfield by adding:

  $form['body_field']['#suffix'] .= "<script type='text/javascript'>var bodybox = $('#edit-body'); bodybox[0].innerHTML = 'This is the default text for body when creating new nodes.';</script>";

Ét voilà: Upon creating a new node the body field is prepopulated with whatever text you entered in the code line above.

Soon you will notice, though, that this also works when editing existing nodes and thus is replacing original text by this line. So we'll need to check if the body's default value is set or not:

  if ( is_null($form['body_field']['body']['#default_value']) ) {
    $form['body_field']['#suffix'] .= "<script type='text/javascript'>var bodybox = $('#edit-body'); bodybox[0].innerHTML = 'This is the default text for body when creating new nodes.';</script>";
  }

Now, the default text is only inserted upon creating new nodes and not when editing existing ones (as long as they contain text).
Well now, you might think, this is working alright, but I want the default text for special content types only. So, you'll need another check before taking any actions. Let's say, only pages shall have a default text inserted, then the INTERESTING PART would look like this:

  if ( $form['type']['#value'] == 'page' ) {
    if ( is_null($form['body_field']['body']['#default_value']) ) {
      $form['body_field']['#suffix'] .= "<script type='text/javascript'>var bodybox = $('#edit-body'); bodybox[0].innerHTML = 'This is the default text for body when creating new PAGES.';</script>";
    }
  }

That way, you are able to set default texts for each content type there is. All you need is the machine readable name of each content type and this function (see whole function below).

/**
* Theme override for node edit form.
*
* The function is named themename_formid.
*/
function YOURTHEMENAME_node_form($form) {
  // Initialize output variable
  $output = '';
  // Print out the $form array to see all the elements we are working with.
  // Note: This is for the developing phase only. Disable it once you found
  // the variables you want to work on. Devel module required.
  #$output .= dsm($form);
  // Once we know which part of the array we are after we can change it.
  // First though disable the line with dsm($form) in it.

  // insert INTERESTING PART here
  if ( $form['type']['#value'] == 'page' ) {
    if ( is_null($form['body_field']['body']['#default_value']) ) {
      $form['body_field']['#suffix'] .= "<script type='text/javascript'>var bodybox = $('#edit-body'); bodybox[0].innerHTML = 'This is the default text for body when creating new PAGES.';</script>";
    }
  }
  elseif ( $form['type']['#value'] == 'story' ) {
    if ( is_null($form['body_field']['body']['#default_value']) ) {
      $form['body_field']['#suffix'] .= "<script type='text/javascript'>var bodybox = $('#edit-body'); bodybox[0].innerHTML = 'This is the default text for body when creating new STORIES.';</script>";
    }
  }

  // Make sure we call drupal_render() on the entire $form to make sure
  // we still output all of the elements (particularly hidden ones needed
  // for the form to function properly.)
  $form['body_field']['#suffix'] .= "<script type='text/javascript'>var bodybox = $('#edit-body'); bodybox[0].innerHTML = 'This is the default text for body when creating new nodes.';</script>";
  $output .= drupal_render($form);
  return $output;
}

While this might not the optimal solution it works quite well for me, so I thought I'd share it.

Best,
Paul

francewhoa’s picture

The following module is able to do that http://drupal.org/project/DefaultTextForNode

Loving back your Drupal community result in multiple benefits for you