You are here

function field_test_field_storage_write in SimpleTest 7

Implement hook_field_storage_write().

File

tests/field_test.module, line 797

Code

function field_test_field_storage_write($obj_type, $object, $op, $fields) {
  $data = _field_test_storage_data();
  list($id, $vid, $bundle) = field_extract_ids($obj_type, $object);
  foreach ($fields as $field_id) {
    $field = field_info_field_by_id($field_id);
    $field_name = $field['field_name'];
    $field_data =& $data[$field_id];
    $all_languages = field_multilingual_available_languages($obj_type, $field);
    $field_languages = array_intersect($all_languages, array_keys((array) $object->{$field_name}));

    // Delete and insert, rather than update, in case a value was added.
    if ($op == FIELD_STORAGE_UPDATE) {

      // Delete languages present in the incoming $object->$field_name.
      // Delete all languages if $object->$field_name is empty.
      $languages = !empty($object->{$field_name}) ? $field_languages : $all_languages;
      if ($languages) {
        foreach ($field_data['current'] as $key => $row) {
          if ($row->type == $obj_type && $row->entity_id == $id && in_array($row->language, $languages)) {
            unset($field_data['current'][$key]);
          }
        }
        if (isset($vid)) {
          foreach ($field_data['revisions'] as $key => $row) {
            if ($row->type == $obj_type && $row->revision_id == $vid) {
              unset($field_data['revisions'][$key]);
            }
          }
        }
      }
    }
    foreach ($field_languages as $langcode) {
      $items = (array) $object->{$field_name}[$langcode];
      $delta_count = 0;
      foreach ($items as $delta => $item) {
        $row = (object) array(
          'field_id' => $field_id,
          'type' => $obj_type,
          'entity_id' => $id,
          'revision_id' => $vid,
          'bundle' => $bundle,
          'delta' => $delta,
          'deleted' => FALSE,
          'language' => $langcode,
        );
        foreach ($field['columns'] as $column => $attributes) {
          $row->{$column} = isset($item[$column]) ? $item[$column] : NULL;
        }
        $field_data['current'][] = $row;
        if (isset($vid)) {
          $field_data['revisions'][] = $row;
        }
        if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) {
          break;
        }
      }
    }
  }
  _field_test_storage_data($data);
}