hello,

I try to set a default value when I create a "article" node, but nothing is display in the body textarea.
In the template.php file in my "homemade" theme, I have registered the function:

function homemade_theme() {
   return array(
     // The form ID.
     'article_node_form' => array(
       // Forms always take the form argument.
       'arguments' => array('form' => NULL)
     )
   );
}

And in the function, I try to set a default value for the body field.

function homemade_article_node_form($form) {
	$output = '';
	$form['body_field']['body']['#title']="test";
	$form['body_field']['body']['#default_value']= "test";
	$output .= drupal_render($form);
	return $output;
}

There is no problem to change the body title but the textarea stays empty.

Please help me to understand my error.
Jerome

Comments

Jeff Burnz’s picture

From what I recall from my dabbling in FAPI is that textareas can't have default values.

jerome@drupal.org’s picture

Do you have any idea how can I update the body field with a default value?
Jerome

blueflowers’s picture

According to the docs you should be able to do this.

http://api.drupal.org/api/file/developer/topics/forms_api_reference.html...

Although I may have taken this for granted all this time as my default values always seem to be what I've assigned them to be. I just tried this in my own module and sure enough I wasn't able to set the default value to an arbitrary value.

Anyone else have any idea what's going on here?

blueflowers’s picture

Okay I muddled with this, this morning.

This worked fine for me:

function press_articles_form($node) {

  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain('Title'),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#description' => 'The title of this press item or article.',
    '#weight' => -10,
  );
  
  $form['body'] = array(
    '#type' => 'textarea',
    '#title' => check_plain('Article/Press Release Body'),
    '#required' => TRUE,
    '#default_value' => !empty($node->body) ? $node->body : 'test',
    '#description' => 'The body of this press item or article.',
    '#weight' => -1,
  );

  return $form;

}

I'm not quite sure what is so different about your code, but hopefully that'll give you a starting point.

jerome@drupal.org’s picture

OK thank you for your assistance.
I will test it now.

Jerome

Mark B’s picture

I've had the same problem. I eventually figured out that the problem was that my textarea element is in a fieldset (with #tree = true). Moving the form element outside the fieldset showed the default value in the form.

Unfortunately building a simple test to recreate the issue failed to recreate it, so I've not raised a bug, but you may find that moving your textarea out of a fieldset helps.

trunglv’s picture

Try it. It work for me.
$form['body'][$form['language']['#value']][0]['#default_value'] = 'The text that you want.';