Index: includes/entity.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/entity.inc,v
retrieving revision 1.14
diff -u -p -r1.14 entity.inc
--- includes/entity.inc	15 Sep 2010 04:34:26 -0000	1.14
+++ includes/entity.inc	17 Sep 2010 03:59:04 -0000
@@ -904,6 +904,8 @@ class EntityFieldQuery {
    *   @code
    *     foreach ($query->execute() as $entity_type => $entities) {
    *       foreach ($entities as $entity_id => $entity) {
+   *       }
+   *     }
    *   @endcode
    *   Note if the entity type is known, then the following snippet will load
    *   the entities found:
@@ -1110,7 +1112,7 @@ class EntityFieldQuery {
    *   HAVING or WHERE. This is necessary because SQL can't handle WHERE
    *   conditions on aliased columns.
    */
-  public function addCondition(SelectQuery $select_query, $sql_field, $condition, $having = FALSE) {
+  public function addCondition($select_query, $sql_field, $condition, $having = FALSE) {
     $method = $having ? 'havingCondition' : 'condition';
     $like_prefix = '';
     switch ($condition['operator']) {
@@ -1123,5 +1125,60 @@ class EntityFieldQuery {
         $select_query->$method($sql_field, $condition['value'], $condition['operator']);
     }
   }
+}
+
+class EntityFieldUpdateQuery extends EntityFieldQuery {
+  public $updateFields = array();
+
+  public function fields(array $fields) {
+    // UpdateQuery::fields() method name clash.
+    $this->updateFields = $fields;
+    return $this;
+  }
+
+  /**
+   * Determines the query callback to use for this entity query.
+   *
+   * @return
+   *   A callback that can be used with call_user_func().
+   */
+  public function queryCallback() {
+    // Use the override from $this->executeCallback. It can be set either
+    // while building the query, or using hook_entity_query_alter().
+    if (function_exists($this->executeCallback)) {
+      return $this->executeCallback;
+    }
+    // If no override, find the storage engine to be used.
+    foreach ($this->fields as $field) {
+      if (!isset($storage)) {
+        $storage = $field['storage']['module'];
+      }
+      elseif ($storage != $field['storage']['module']) {
+        throw new EntityFieldQueryException(t("Can't handle more than one field storage engine"));
+      }
+    }
+    if ($storage) {
+      // Use hook_field_storage_query() from the field storage.
+      return $storage . '_field_storage_update_query';
+    }
+    else {
+      throw new EntityFieldQueryException(t("Field storage engine not found."));
+    }
+  }
 
+  /**
+   * Finishes the query.
+   *
+   * @param SelectQuery $select_query
+   *   A SelectQuery which has entity_type, entity_id, revision_id and bundle
+   *   fields added.
+   *
+   * @return
+   *   The query result, if any.
+   *
+   * @see EntityFieldQuery::execute().
+   */
+  function finishQuery($db_query, $id_key = 'entity_id') {
+    return $db_query->execute();
+  }
 }
Index: modules/field/field.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/field.module,v
retrieving revision 1.84
diff -u -p -r1.84 field.module
--- modules/field/field.module	5 Sep 2010 02:21:38 -0000	1.84
+++ modules/field/field.module	17 Sep 2010 03:59:04 -0000
@@ -98,6 +98,7 @@ define('FIELD_BEHAVIOR_CUSTOM', 0x0004);
  * field data with field_attach_load().
  */
 define('FIELD_LOAD_CURRENT', 'FIELD_LOAD_CURRENT');
+
 /**
  * Age argument for loading the version of an entity's field data
  * specified in the entity with field_attach_load().
Index: modules/field/modules/field_sql_storage/field_sql_storage.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/modules/field_sql_storage/field_sql_storage.module,v
retrieving revision 1.52
diff -u -p -r1.52 field_sql_storage.module
--- modules/field/modules/field_sql_storage/field_sql_storage.module	5 Sep 2010 20:00:45 -0000	1.52
+++ modules/field/modules/field_sql_storage/field_sql_storage.module	17 Sep 2010 03:59:04 -0000
@@ -478,6 +478,37 @@ function field_sql_storage_field_storage
 }
 
 /**
+ * Add conditions to a query.
+ *
+ * @param $query
+ *   An EntityFieldQuery object.
+ * @param $db_query
+ *   The object of the actually performed query.
+ */
+function _field_sql_storage_add_conditions(EntityFieldQuery $query, $db_query) {
+  // Add field conditions.
+  foreach ($query->fieldConditions as $key => $condition) {
+    $table_alias = $query->fields[$key]['alias'];
+    $field = $condition['field'];
+    // Add the specified condition.
+    $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $condition['column']);
+    $query->addCondition($db_query, $sql_field, $condition);
+    // Add delta / language group conditions.
+    foreach (array('delta', 'language') as $column) {
+      if (isset($condition[$column . '_group'])) {
+        $group_name = $condition[$column . '_group'];
+        if (!isset($groups[$column][$group_name])) {
+          $groups[$column][$group_name] = $table_alias;
+        }
+        else {
+          $db_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column");
+        }
+      }
+    }
+  }
+}
+
+/**
  * Implements hook_field_storage_query().
  */
 function field_sql_storage_field_storage_query(EntityFieldQuery $query) {
@@ -490,13 +521,13 @@ function field_sql_storage_field_storage
     $tablename_function = '_field_sql_storage_revision_tablename';
     $id_key = 'revision_id';
   }
-  $table_aliases = array();
+
   // Add tables for the fields used.
-  foreach ($query->fields as $key => $field) {
+  foreach ($query->fields as $key => &$field) {
     $tablename = $tablename_function($field);
     // Every field needs a new table.
     $table_alias = $tablename . $key;
-    $table_aliases[$key] = $table_alias;
+    $field['alias'] = $table_alias;
     if ($key) {
       $select_query->join($tablename, $table_alias, "$table_alias.etid = $field_base_table.etid AND $table_alias.$id_key = $field_base_table.$id_key");
     }
@@ -518,29 +549,11 @@ function field_sql_storage_field_storage
   }
 
   // Add field conditions.
-  foreach ($query->fieldConditions as $key => $condition) {
-    $table_alias = $table_aliases[$key];
-    $field = $condition['field'];
-    // Add the specified condition.
-    $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $condition['column']);
-    $query->addCondition($select_query, $sql_field, $condition);
-    // Add delta / language group conditions.
-    foreach (array('delta', 'language') as $column) {
-      if (isset($condition[$column . '_group'])) {
-        $group_name = $condition[$column . '_group'];
-        if (!isset($groups[$column][$group_name])) {
-          $groups[$column][$group_name] = $table_alias;
-        }
-        else {
-          $select_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column");
-        }
-      }
-    }
-  }
+  _field_sql_storage_add_conditions($query, $select_query);
 
   // Add field orders.
   foreach ($query->fieldOrder as $key => $order) {
-    $table_alias = $table_aliases[$key];
+    $table_alias = $query->fields[$key]['alias'];
     $field = $order['field'];
     $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $order['column']);
     $select_query->orderBy($sql_field, $order['direction']);
@@ -594,6 +607,40 @@ function _field_sql_storage_query_join_e
 }
 
 /**
+ * Implements hook_field_storage_update_query().
+ */
+function field_sql_storage_field_storage_update_query(EntityFieldUpdateQuery $query) {
+  // @todo WTF? Query only executed for one of both currently.
+  if ($query->age == FIELD_LOAD_CURRENT) {
+    $tablename_function = '_field_sql_storage_tablename';
+    $id_key = 'entity_id';
+  }
+  else {
+    $tablename_function = '_field_sql_storage_revision_tablename';
+    $id_key = 'revision_id';
+  }
+
+  // Add tables for the fields used.
+  $result = array();
+  foreach ($query->fields as $key => &$field) {
+    $tablename = $tablename_function($field);
+    $field['alias'] = $tablename;
+    $db_query = db_update($tablename);
+    // @todo WTF?
+    $columns = $field['storage']['details']['sql'][$query->age][$tablename];
+    $fields = array();
+    foreach ($query->updateFields as $name => $value) {
+      $fields[$columns[$name]] = $value;
+    }
+    $db_query->fields($fields);
+    // Add field conditions.
+    _field_sql_storage_add_conditions($query, $db_query);
+    $result[$key] = $query->finishQuery($db_query, $id_key);
+  }
+  return $result;
+}
+
+/**
  * Implements hook_field_storage_delete_revision().
  *
  * This function actually deletes the data from the database.
Index: modules/field/modules/text/text.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/modules/text/text.module,v
retrieving revision 1.64
diff -u -p -r1.64 text.module
--- modules/field/modules/text/text.module	11 Sep 2010 04:19:15 -0000	1.64
+++ modules/field/modules/text/text.module	17 Sep 2010 03:59:04 -0000
@@ -606,10 +606,21 @@ function text_filter_format_update() {
 
 /**
  * Implements hook_filter_format_delete().
- *
- * @todo D8: Properly update filter format references in all fields. See
- *   http://drupal.org/node/556022 for details.
  */
-function text_filter_format_delete() {
+function text_filter_format_delete($format, $replacement) {
+  $query = new EntityFieldUpdateQuery();
+  $query
+    ->fields(array('format' => $replacement->format))
+    ->age(FIELD_LOAD_REVISION);
+
+  $fields = field_info_fields();
+  foreach ($fields as $field_name => $field) {
+    if ($field['module'] == 'text') {
+      $field_query = clone $query;
+      $field_query
+        ->fieldCondition($field, 'format', $format->format)
+        ->execute();
+    }
+  }
   field_cache_clear();
 }
Index: modules/field/modules/text/text.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/modules/text/text.test,v
retrieving revision 1.27
diff -u -p -r1.27 text.test
--- modules/field/modules/text/text.test	17 Aug 2010 18:25:31 -0000	1.27
+++ modules/field/modules/text/text.test	17 Sep 2010 03:59:04 -0000
@@ -226,6 +226,14 @@ class TextFieldTestCase extends DrupalWe
     $entity->content = field_attach_view($entity_type, $entity, 'full');
     $this->content = drupal_render($entity->content);
     $this->assertRaw($value, t('Value is displayed unfiltered'));
+
+    // Delete the text format and verify the field content renders in
+    // plain text.
+    filter_format_delete(filter_format_load($format_id));
+    $entity = field_test_entity_test_load($id);
+    $entity->content = field_attach_view($entity_type, $entity);
+    $this->content = drupal_render($entity->content);
+    $this->assertRaw(check_plain($value), t('Value is displayed in plain text when text format is deleted'));
   }
 }
 
