You are here

function media_embed_file_usage_parse in Media WYSIWYG Embed 7

Parse file references from an entity's text fields and return them as an array.

Parameters

string $entity_type: Entity type.

object $entity: Entity object.

Return value

array An associative array where keys are file IDs and values are file usage count.

2 calls to media_embed_file_usage_parse()
media_embed_field_attach_delete_revision in includes/media_embed.file_usage.inc
Implements hook_field_attach_delete_revision().
media_embed_file_usage_update in includes/media_embed.file_usage.inc
Add file usage from file references in an entity's text fields.

File

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

Code

function media_embed_file_usage_parse($entity_type, $entity) {
  $references = array();
  $fields = media_embed_text_processing_fields($entity_type, $entity);
  foreach ($fields as $field_name) {
    $field_items = field_get_items($entity_type, $entity, $field_name);
    if ($field_items) {
      foreach ($field_items as $field_item) {
        preg_match_all('#' . MEDIA_EMBED_TOKEN_PATTERN . '#', $field_item['value'], $matches);
        foreach ($matches[1] as $id) {
          $file = file_load($id);
          if ($file) {
            isset($references[$id]) ? ++$references[$id] : ($references[$id] = 1);
          }
        }
      }
    }
  }
  return $references;
}