You are here

function media_embed_file_usage_update in Media WYSIWYG Embed 7

Add file usage from file references in an entity's text fields.

Parameters

string $entity_type: Entity type.

object $entity: Entity object.

2 calls to media_embed_file_usage_update()
media_embed_field_attach_insert in includes/media_embed.file_usage.inc
Implements hook_field_attach_insert().
media_embed_field_attach_update in includes/media_embed.file_usage.inc
Implements hook_field_attach_update().

File

includes/media_embed.file_usage.inc, line 59
Functions related to the tracking the file usage of embedded media.

Code

function media_embed_file_usage_update($entity_type, $entity) {
  if (!empty($entity->file_usage_processed['media_embed'])) {
    return;
  }
  $references = media_embed_file_usage_parse($entity_type, $entity);
  list($entity_id) = entity_extract_ids($entity_type, $entity);
  if (empty($entity->revision) && empty($entity->old_vid) && empty($entity->is_new) && !empty($entity->original)) {
    $old_references = media_embed_file_usage_parse($entity_type, $entity->original);
    foreach ($old_references as $id => $old_count) {
      $count = !empty($references[$id]) ? $references[$id] : 0;
      if ($old_count > $count) {
        $deprecate = $old_count - $count;
        $file = file_load($id);
        $file && file_usage_delete($file, 'media_embed', $entity_type, $entity_id, $deprecate);
        unset($references[$id]);
      }
      elseif ($old_count == $count) {
        unset($references[$id]);
      }
      else {
        $references[$id] = $count - $old_count;
      }
    }
  }
  foreach ($references as $id => $count) {
    $file = file_load($id);
    $file && file_usage_add($file, 'media_embed', $entity_type, $entity_id, $count);
  }
  $entity->file_usage_processed['media_embed'] = TRUE;
}