You are here

function text_field_update in Scald: Media Management made easy 7

Implements hook_field_update() on behalf of Text module.

See also

text_field_presave()

File

modules/fields/mee/mee.module, line 324
Defines a special textarea, with drag and drop media driven by Scald and dnd.module.

Code

function text_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  if (!_mee_field_instance_enabled($instance, 'mee')) {
    return;
  }
  list($id, $revision_id) = _mee_extract_id($entity_type, $entity);
  foreach ($items as $delta => $item) {
    list($sids, $copyrights) = _mee_process_item_value($item, $entity_type, $entity, $field, $delta);

    // In fact, we'll delete all the associations and recreate afterwards
    // the needed one, to be sure that new resources are correctly
    // registered, and that no longer used one are removed.
    db_delete('mee_resource')
      ->condition('entity_type', $entity_type)
      ->condition('entity_id', $id)
      ->condition('revision_id', $revision_id)
      ->condition('field', $field['field_name'])
      ->condition('delta', $delta)
      ->execute();

    // Normalize the weight, putting our separator at 0.
    $separator = $item['mee']['resource_manager'][0]['weight'];
    foreach ($sids as $sid) {
      $resource = $item['mee']['resource_manager'][$sid];
      db_insert('mee_resource')
        ->fields(array(
        'entity_type' => $entity_type,
        'entity_id' => $id,
        'revision_id' => $revision_id,
        'atom_sid' => $sid,
        'field' => $field['field_name'],
        'delta' => $delta,
        'weight' => $resource['weight'] - $separator,
        'required' => isset($resource['required']) ? (int) $resource['required'] : 0,
        'copyright' => isset($copyrights[$sid]) ? $copyrights[$sid] : '',
      ))
        ->execute();
    }
  }
}