You are here

function vsf_wysiwyg_field_attach_update in Visual select file 7

Implements hook_field_attach_update().

1 call to vsf_wysiwyg_field_attach_update()
vsf_wysiwyg_field_attach_insert in submodules/vsf_wysiwyg/vsf_wysiwyg.module
Implements hook_field_attach_insert().

File

submodules/vsf_wysiwyg/vsf_wysiwyg.module, line 207

Code

function vsf_wysiwyg_field_attach_update($entity_type, $entity) {
  if (!isset($entity->vsf_wysiwyg_fids)) {
    return;
  }
  list($id, , $bundle) = entity_extract_ids($entity_type, $entity);

  // Load current usage for this entity.
  $current_usage = db_query('
    SELECT fid, count
    FROM {file_usage}
    WHERE module = :module AND type = :type AND id = :id
    ORDER BY fid ASC
  ', array(
    ':module' => 'vsf_wysiwyg',
    ':type' => $entity_type,
    ':id' => $id,
  ))
    ->fetchAllKeyed(0, 1);

  // Compare old and new file usage and remove and add. If they differ at all, redo all usage. No point
  // in spending more effort to find the exact difference.
  if ($entity->vsf_wysiwyg_fids != $current_usage) {

    // Delete all, for this entity.
    db_delete('file_usage')
      ->condition('module', 'vsf_wysiwyg')
      ->condition('type', $entity_type)
      ->condition('id', $id)
      ->execute();

    // Add per found fid.
    foreach ($entity->vsf_wysiwyg_fids as $fid => $count) {
      $file = (object) compact('fid');
      file_usage_add($file, 'vsf_wysiwyg', $entity_type, $id, $count);
    }
    watchdog('vsf_wysiwyg', format_string('Updating VSF WYSIWYG file usage for !type # !id', array(
      '!type' => $entity_type,
      '!id' => $id,
    )));
  }
}