Changing a “textarea” field's default input format

Recently, I was trying to find a simple way to change the default input format of fields to 'Full HTML'.
I created a hook_form_alter() function in my module and for my content type body, I wrote the following:
<?php
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'MYCONTENT_node_form':
      // Set default body field input format to Full HTML
      foreach(element_children($form['body_field']['format']) as $key) {
        $form['body_field']['format'][$key]['#default_value'] = '2';
      }
    break;
  }
}
?>

Now, I wanted to do the same for the CCK “textarea” field on that node type, but was struggling with it, since the CCK fields work differently.

I figured out (thanks to Drupal's documentation at http://drupal.org/node/726282) that I need to address the CCK field differently, but I couldn’t find an explanation in the documentation of how to set the default input format. After playing around with the code for awhile, I found the answer, which is very simple, as you can see in the following code:
<?php
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'MYCONTENT_node_form':
      // Set default body field input format to Full HTML
      foreach(element_children($form['body_field']['format']) as $key) {
        $form['body_field']['format'][$key]['#default_value'] = '2';
      }
      // Set default cck field input format to Full HTML
      foreach(element_children($form['field_MYCCKFIELD']) as $key) {
        $form['field_MYCCKFIELD'][$key]['#default_value']['format'] = 2;
      }
      break;
  }
}
?>
The integer '2' is for Full HTML format and '1' is for Filtered HTML format.  Other formats can be used depending on the settings of your input formats.
 

תגובות

Basil

Great post! I really like the idea of this site as it contains lots of useful information that I can take from here.That was a nice one. Really drupal do best.

פרסום תגובה חדשה