### Eclipse Workspace Patch 1.0
#P drupal_test_7
Index: modules/field/modules/text/text.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/modules/text/text.test,v
retrieving revision 1.3
diff -u -r1.3 text.test
--- modules/field/modules/text/text.test	31 Mar 2009 01:49:52 -0000	1.3
+++ modules/field/modules/text/text.test	4 Apr 2009 00:29:26 -0000
@@ -14,6 +14,9 @@
 
   function setUp() {
     parent::setUp('field', 'text', 'field_test');
+
+    $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
+    $this->drupalLogin($web_user);
   }
 
   // Test fields.
@@ -57,6 +60,83 @@
    * Test textarea widget.
    */
 
+
+  /**
+   * Test 'formatted_text' setting.
+   */
+  function testTextfieldFormattedForm() {
+    $this->_testTextfieldFormattedTextForm('text', 'text_textfield');
+    $this->_testTextfieldFormattedTextForm('text_long', 'text_textarea');
+  }
+
+  /**
+   * Helper function for testTextfieldFormattedForm().
+   */
+  function _testTextfieldFormattedTextForm($field_type, $widget_type) {
+    $entity_type = 'test_entity';
+    $this->field_name = drupal_strtolower($this->randomName(). '_field_name');
+    $this->field = array('field_name' => $this->field_name, 'type' => $field_type);
+    field_create_field($this->field);
+    $this->instance = array(
+      'field_name' => $this->field_name,
+      'bundle' => FIELD_TEST_BUNDLE,
+      'label' => $this->randomName(). '_label',
+      'settings' => array(
+        'text_processing' => TRUE,
+      ),
+      'widget' => array(
+        'type' => $widget_type,
+      )
+    );
+    field_create_instance($this->instance);
+
+    // Display creation form.
+    // By default, the user only has access to 'Filtered HTML', and no format
+    // selector is displayed
+    $this->drupalGet('test-entity/add/test-bundle');
+    $this->assertFieldByName($this->field_name . '[0][value]', '', t('Widget is displayed'));
+    $this->assertNoFieldByName($this->field_name . '[0][value_format]', '1', t('Format selector is not displayed'));
+
+    // Submit with data that should be filtered.
+    $value = $this->randomName() . '<br />' . $this->randomName();
+    $edit = array(
+      $this->field_name . '[0][value]' => $value,
+    );
+    $this->drupalPost(NULL, $edit, t('Save'));
+    preg_match('|test-entity/(\d+)/edit|', $this->url, $match);
+    $id = $match[1];
+    $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Entity was created'));
+
+    // Display the object.
+    $entity = field_test_entity_load($id);
+    $entity->content = field_attach_view($entity_type, $entity);
+    $this->content = drupal_render($entity->content);
+    $this->assertNoRaw($value, 'Filtered tags are not displayed');
+    $this->assertRaw(str_replace('<br />', '', $value), t('Filtered value is displayed correctly'));
+
+    // Allow the user to use the 'Full HTML' format.
+    db_update('filter_format')->fields(array('roles' => ',2,'))->condition('format', 2)->execute();
+
+    // Display edition form.
+    // We should now have a 'text format' selector.
+    $this->drupalGet('test-entity/' . $id . '/edit');
+    $this->assertFieldByName($this->field_name . '[0][value]', '', t('Widget is displayed'));
+    $this->assertFieldByName($this->field_name . '[0][value_format]', '1', t('Format selector is displayed'));
+
+    // Edit and change the format to 'Full HTML'.
+    $edit = array(
+      $this->field_name . '[0][value_format]' => 2,
+    );
+    $this->drupalPost(NULL, $edit, t('Save'));
+    $this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), t('Entity was updated'));
+
+    // Display the object.
+    $entity = field_test_entity_load($id);
+    $entity->content = field_attach_view($entity_type, $entity);
+    $this->content = drupal_render($entity->content);
+    $this->assertRaw($value, t('Value is displayed unfiltered'));
+  }
+
   // Test formatters.
   /**
    *
Index: modules/field/modules/text/text.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/modules/text/text.module,v
retrieving revision 1.5
diff -u -r1.5 text.module
--- modules/field/modules/text/text.module	26 Mar 2009 13:31:25 -0000	1.5
+++ modules/field/modules/text/text.module	4 Apr 2009 00:29:26 -0000
@@ -105,13 +105,16 @@
   }
 }
 
+/**
+ * Implementation of hook_field_sanitize().
+ */
 function text_field_sanitize($obj_type, $object, $field, $instance, &$items) {
   global $language;
   foreach ($items as $delta => $item) {
     // TODO D7 : this code is really node-related.
     if (!empty($instance['settings']['text_processing'])) {
       $check = is_null($object) || (isset($object->build_mode) && $object->build_mode == NODE_BUILD_PREVIEW);
-      $text = isset($item['value']) ? check_markup($item['value'], $item['format'], isset($object->language) ? $object->language : $language, $check) : '';
+      $text = isset($item['value']) ? check_markup($item['value'], $item['format'], isset($object->language) ? $object->language : $language->language, $check) : '';
     }
     else {
       $text = check_plain($item['value']);
@@ -235,13 +238,12 @@
       '#columns' => array('value'), '#delta' => 0,
       '#process' => array('text_textfield_process'),
       '#autocomplete_path' => FALSE,
-      ),
+    ),
     'text_textarea' => array(
       '#input' => TRUE,
       '#columns' => array('value', 'format'), '#delta' => 0,
       '#process' => array('text_textarea_process'),
-      '#filter_value' => FILTER_FORMAT_DEFAULT,
-      ),
+    ),
   );
 }
 
@@ -282,6 +284,9 @@
     '#type' => $instance['widget']['type'],
     '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
   );
+  if (!empty($instance['settings']['text_processing'])) {
+    $element['#value_callback'] = 'text_field_widget_formatted_text_value';
+  }
   return $element;
 }
 
@@ -331,10 +336,9 @@
   $element[$field_key]['#maxlength'] = !empty($field['settings']['max_length']) ? $field['settings']['max_length'] : NULL;
 
   if (!empty($instance['settings']['text_processing'])) {
-    $filter_key = $element['#columns'][1];
+    $filter_key = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
     $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
-    $parents = array_merge($element['#parents'] , array($filter_key));
-    $element[$filter_key] = filter_form($format, 1, $parents);
+    $element[$field_key]['#text_format'] = $format;
   }
 
   return $element;
@@ -372,8 +376,28 @@
   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;
-    $parents = array_merge($element['#parents'] , array($filter_key));
-    $element[$filter_key] = filter_form($format, 1, $parents);
+    $element[$field_key]['#text_format'] = $format;
+  }
+
+  return $element;
+}
+
+/**
+ * Helper function to determine the value for a formatted text widget.
+ *
+ * '#text_format' puts the format in '[column 0]_format' in incoming values,
+ * while we need it in '[column 1]'.
+ */
+function text_field_widget_formatted_text_value($form, $edit = FALSE) {
+  if ($edit !== FALSE) {
+    $field_key = $form['#columns'][0];
+    $filter_key = (count($form['#columns']) == 2) ? $form['#columns'][1] : 'format';
+    $default_key = $field_key . '_format';
+    if (!empty($edit[$default_key])) {
+      $edit['format'] = $edit[$default_key];
+      unset($edit[$default_key]);
+    }
+    return $edit;
   }
 }
 
