Index: includes/form.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/form.inc,v
retrieving revision 1.370
diff -u -p -r1.370 form.inc
--- includes/form.inc	5 Sep 2009 15:05:01 -0000	1.370
+++ includes/form.inc	9 Sep 2009 01:53:15 -0000
@@ -1884,107 +1884,6 @@ function form_process_radios($element) {
 }
 
 /**
- * Add text format selector to text elements with the #text_format property.
- *
- * The #text_format property should be the ID of an text format, found in
- * {filter_format}.format, which gets passed to filter_form().
- *
- * If the property #text_format is set, the form element will be expanded into
- * two separate form elements, one holding the content of the element, and the
- * other holding the text format selector. The original element is shifted into
- * a child element, but is otherwise unaltered, so that the format selector is
- * at the same level as the text field which it affects.
- *
- * For example:
- * @code
- *   // A simple textarea, such as a node body.
- *   $form['body'] = array(
- *     '#type' => 'textarea',
- *     '#title' => t('Body'),
- *     '#text_format' => isset($node->format) ? $node->format : FILTER_FORMAT_DEFAULT,
- *   );
- * @endcode
- *
- * Becomes:
- * @code
- *   $form['body'] = array(
- *     // Type switches to 'markup', as we're only interested in submitting the child elements.
- *     '#type' => 'markup',
- *     // 'value' holds the original element.
- *     'value' => array(
- *       '#type' => 'textarea',
- *       '#title' => t('Body'),
- *       '#parents' => array('body'),
- *     ),
- *     // 'format' holds the text format selector.
- *     'format' => array(
- *       '#parents' => array('body_format'),
- *       ...
- *     ),
- *   );
- * @endcode
- *
- * And would result in:
- * @code
- *   // Original, unaltered form element value.
- *   $form_state['values']['body'] = 'Example content';
- *   // Chosen text format.
- *   $form_state['values']['body_format'] = 1;
- * @endcode
- *
- * @see system_elements(), filter_form()
- */
-function form_process_text_format($element) {
-  if (isset($element['#text_format'])) {
-    // Determine the form element parents and element name to use for the input
-    // format widget. This simulates the 'element' and 'element_format' pair of
-    // parents that filter_form() expects.
-    $element_parents = $element['#parents'];
-    $element_name = array_pop($element_parents);
-    $element_parents[] = $element_name . '_format';
-
-    // We need to break references, otherwise form_builder recurses infinitely.
-    $element['value'] = (array)$element;
-    $element['value']['#weight'] = 0;
-    unset($element['value']['#description']);
-    $element['#type'] = 'markup';
-    $element['#theme'] = NULL;
-    $element['#theme_wrappers'] = array('text_format_wrapper');
-    $element['format'] = filter_form($element['#text_format'], 1, $element_parents);
-
-    // We need to clear the #text_format from the new child otherwise we
-    // would get into an infinite loop.
-    unset($element['value']['#text_format']);
-  }
-  return $element;
-}
-
-/**
- * Theme a text format form element.
- *
- * @param element
- *   An associative array containing the properties of the element.
- *   Properties used: #children, #description
- * @return
- *   A string representing the form element.
- *
- * @ingroup themeable
- */
-function theme_text_format_wrapper($element) {
-  $output = '<div class="text-format-wrapper">' . "\n";
-
-  $output .= $element['#children'] . "\n";
-
-  if (!empty($element['#description'])) {
-    $output .= '<div class="description">' . $element['#description'] . "</div>\n";
-  }
-
-  $output .= "</div>\n";
-
-  return $output;
-}
-
-/**
  * Theme a checkbox form element.
  *
  * @param $element
Index: modules/block/block.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/block/block.module,v
retrieving revision 1.373
diff -u -p -r1.373 block.module
--- modules/block/block.module	31 Aug 2009 17:06:08 -0000	1.373
+++ modules/block/block.module	9 Sep 2009 02:31:47 -0000
@@ -346,10 +346,10 @@ function block_custom_block_form($edit =
   );
   $form['body_field']['#weight'] = -17;
   $form['body_field']['body'] = array(
-    '#type' => 'textarea',
+    '#type' => 'textarea_format',
     '#title' => t('Block body'),
     '#default_value' => $edit['body'],
-    '#text_format' => isset($edit['format']) ? $edit['format'] : FILTER_FORMAT_DEFAULT,
+    '#format' => isset($edit['format']) ? $edit['format'] : FILTER_FORMAT_DEFAULT,
     '#rows' => 15,
     '#description' => t('The content of the block as shown to the user.'),
     '#required' => TRUE,
Index: modules/comment/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v
retrieving revision 1.767
diff -u -p -r1.767 comment.module
--- modules/comment/comment.module	5 Sep 2009 15:05:02 -0000	1.767
+++ modules/comment/comment.module	9 Sep 2009 02:32:48 -0000
@@ -1806,11 +1806,11 @@ function comment_form(&$form_state, $com
   }
 
   $form['comment'] = array(
-    '#type' => 'textarea',
+    '#type' => 'textarea_format',
     '#title' => t('Comment'),
     '#rows' => 15,
     '#default_value' => $default,
-    '#text_format' => isset($comment->format) ? $comment->format : FILTER_FORMAT_DEFAULT,
+    '#format' => isset($comment->format) ? $comment->format : FILTER_FORMAT_DEFAULT,
     '#required' => TRUE,
   );
 
Index: modules/field/modules/text/text.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/modules/text/text.module,v
retrieving revision 1.25
diff -u -p -r1.25 text.module
--- modules/field/modules/text/text.module	27 Aug 2009 00:33:52 -0000	1.25
+++ modules/field/modules/text/text.module	9 Sep 2009 02:36:02 -0000
@@ -668,7 +668,8 @@ function text_textfield_elements_process
   if (!empty($instance['settings']['text_processing'])) {
     $filter_key  = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
     $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
-    $element[$field_key]['#text_format'] = $format;
+    $element[$field_key]['#type'] .= '_format';
+    $element[$field_key]['#format'] = $format;
   }
 
   return $element;
@@ -701,7 +702,8 @@ function text_textarea_elements_process(
   if (!empty($instance['settings']['text_processing'])) {
     $filter_key = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
     $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
-    $element[$field_key]['#text_format'] = $format;
+    $element[$field_key]['#type'] .= '_format';
+    $element[$field_key]['#format'] = $format;
   }
 
   return $element;
@@ -749,7 +751,8 @@ function text_textarea_with_summary_proc
   if (!empty($instance['settings']['text_processing'])) {
     $filter_key  = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
     $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
-    $element[$field_key]['#text_format'] = $format;
+    $element[$field_key]['#type'] .= '_format';
+    $element[$field_key]['#format'] = $format;
   }
 
   return $element;
@@ -758,8 +761,10 @@ function text_textarea_with_summary_proc
 /**
  * Helper function to determine the value for a formatted text widget.
  *
- * '#text_format' puts the format in '[column 0]_format' in incoming values,
+ * The format is put into '[column 0]_format' in incoming values,
  * while we need it in '[column 1]'.
+ *
+ * @todo Most probably obsolete.
  */
 function text_field_widget_formatted_text_value($form, $edit = FALSE) {
   if ($edit !== FALSE) {
Index: modules/filter/filter.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/filter/filter.module,v
retrieving revision 1.287
diff -u -p -r1.287 filter.module
--- modules/filter/filter.module	30 Aug 2009 06:04:09 -0000	1.287
+++ modules/filter/filter.module	9 Sep 2009 03:54:48 -0000
@@ -67,6 +67,28 @@ function filter_theme() {
 }
 
 /**
+ * Implement of hook_element_info_alter().
+ *
+ * Elements defined by Filter module are wrappers around the respective,
+ * non-prefixed elements, extended by additionally required properties for
+ * filter_process_format().
+ *
+ * For better maintenance, stub element types are inherited from their
+ * respective, original element types.
+ *
+ * @see filter_process_format()
+ */
+function filter_element_info_alter(&$type) {
+  $type['textfield_format'] = $type['textfield'];
+  $type['textfield_format']['#format'] = FILTER_FORMAT_DEFAULT;
+  array_unshift($type['textfield_format']['#process'], 'filter_process_format');
+
+  $type['textarea_format'] = $type['textarea'];
+  $type['textarea_format']['#format'] = FILTER_FORMAT_DEFAULT;
+  array_unshift($type['textfield_format']['#process'], 'filter_process_format');
+}
+
+/**
  * Implement hook_menu().
  */
 function filter_menu() {
@@ -589,28 +611,102 @@ function check_markup($text, $format = F
 /**
  * Generate a selector for choosing a format in a form.
  *
- * @ingroup forms
- * @param $selected_format
- *   The ID of the format that is currently selected.
+ * The form element will be expanded into two separate form elements, one
+ * holding the content of the element, and the other holding the text format
+ * selector. The original element is shifted into a child element, but is
+ * otherwise unaltered, so that the format selector is at the same level as the
+ * text field which it affects.
+ *
+ * The optional #format property should be the ID of an text format, found in
+ * {filter_format}.format. By default, FILTER_FORMAT_DEFAULT is used.
+ *
+ * For example:
+ * @code
+ *   // A simple textarea, such as a node body.
+ *   $form['body'] = array(
+ *     '#type' => 'textarea_format',
+ *     '#title' => t('Body'),
+ *     '#format' => isset($node->format) ? $node->format : NULL,
+ *   );
+ * @endcode
+ *
+ * Becomes:
+ * @code
+ *   $form['body'] = array(
+ *     // Type switches to 'markup', as we're only interested in submitting the child elements.
+ *     '#type' => 'markup',
+ *     // 'value' holds the original element.
+ *     'value' => array(
+ *       '#type' => 'textarea',
+ *       '#title' => t('Body'),
+ *       '#parents' => array('body'),
+ *     ),
+ *     // 'format' holds the text format selector.
+ *     'format' => array(
+ *       '#parents' => array('body_format'),
+ *       ...
+ *     ),
+ *   );
+ * @endcode
+ *
+ * And would result in:
+ * @code
+ *   // Original, unaltered form element value.
+ *   $form_state['values']['body'] = 'Example content';
+ *   // Chosen text format.
+ *   $form_state['values']['body_format'] = 1;
+ * @endcode
+ *
  * @param $weight
  *   The weight of the text format.
  * @param $parents
- *   Required when defining multiple text formats on a single node or having a different parent than 'format'.
+ *   Required when defining multiple text formats on a single node or having a
+ *   different parent than 'format'.
+ *
  * @return
  *   HTML for the form element.
+ *
+ * @ingroup forms
  */
-function filter_form($selected_format = FILTER_FORMAT_DEFAULT, $weight = NULL, $parents = array('format')) {
-  $selected_format = filter_resolve_format($selected_format);
+function filter_process_format($element) {
+  // Determine the form element parents and element name to use for the text
+  // format widget.
+  if (isset($element['#format_parents'])) {
+    $parents = $element['#format_parents'];
+  }
+  else {
+    $parents = $element['#parents'];
+    $element_name = array_pop($parents);
+    $parents[] = $element_name . '_format';
+  }
+
+  // Inject the real form element widget, f.e. a textarea.
+  // We need to break references, otherwise form_builder() recurses infinitely.
+  $element['value'] = (array)$element;
+  $element['value']['#type'] = str_replace('_format', '', $element['value']['#type']);
+  $element['value']['#weight'] = 0;
+  // The form element's description is handled in theme_text_format_wrapper().
+  unset($element['value']['#description']);
+  // We need to clear the #process from the new child, otherwise we would run
+  // into an infinite loop.
+  unset($element['value']['#process'][array_search('filter_process_format', $element['value']['#process'])]);
+
+  // Make the stub element output the text format wrapper instead.
+  $element['#type'] = 'markup';
+  $element['#theme'] = NULL;
+  $element['#theme_wrappers'] = array('text_format_wrapper');
+
+  $selected_format = filter_resolve_format($element['#format']);
   $formats = filter_formats();
 
-  drupal_add_js('misc/form.js');
-  drupal_add_css(drupal_get_path('module', 'filter') . '/filter.css');
   $element_id = form_clean_id('edit-' . implode('-', $parents));
 
   $form = array(
     '#type' => 'fieldset',
-    '#weight' => $weight,
+    '#weight' => isset($element['#weight']) ? $element['#weight'] : NULL,
     '#attributes' => array('class' => array('filter-wrapper')),
+    '#attached_js' => array('misc/form.js'),
+    '#attached_css' => array(drupal_get_path('module', 'filter') . '/filter.css'),
   );
   $form['format_guidelines'] = array(
     '#prefix' => '<div id="' . $element_id . '-guidelines" class="filter-guidelines">',
@@ -640,7 +736,30 @@ function filter_form($selected_format = 
     '#weight' => 1,
   );
 
-  return $form;
+  $element['format'] = $form;
+
+  return $element;
+}
+
+/**
+ * Theme a text format form element.
+ *
+ * @param $element
+ *   An associative array containing the properties of the element.
+ *   Properties used: #children, #description
+ * @return
+ *   A string representing the form element.
+ *
+ * @ingroup themeable
+ */
+function theme_text_format_wrapper($element) {
+  $output = '<div class="text-format-wrapper">' . $element['#children'];
+  if (!empty($element['#description'])) {
+    $output .= '<div class="description">' . $element['#description'] . '</div>';
+  }
+  $output .= "</div>\n";
+
+  return $output;
 }
 
 /**
Index: modules/system/system.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.module,v
retrieving revision 1.785
diff -u -p -r1.785 system.module
--- modules/system/system.module	5 Sep 2009 15:05:04 -0000	1.785
+++ modules/system/system.module	9 Sep 2009 00:39:45 -0000
@@ -338,7 +338,7 @@ function system_elements() {
     '#size' => 60,
     '#maxlength' => 128,
     '#autocomplete_path' => FALSE,
-    '#process' => array('form_process_text_format', 'ajax_process_form'),
+    '#process' => array('ajax_process_form'),
     '#theme' => 'textfield',
     '#theme_wrappers' => array('form_element'),
   );
@@ -363,7 +363,7 @@ function system_elements() {
     '#cols' => 60,
     '#rows' => 5,
     '#resizable' => TRUE,
-    '#process' => array('form_process_text_format', 'ajax_process_form'),
+    '#process' => array('ajax_process_form'),
     '#theme' => 'textarea',
     '#theme_wrappers' => array('form_element'),
   );
