diff --git a/includes/media.filter.inc b/includes/media.filter.inc index e34b6ec..9a541f9 100644 --- a/includes/media.filter.inc +++ b/includes/media.filter.inc @@ -7,6 +7,8 @@ * @TODO: Rename this file? */ +define('MEDIA_TOKEN_REGEX', '/\[\[.*?]]/s'); + /** * Implements hook_wysiwyg_include_directory(). */ @@ -26,7 +28,7 @@ function media_wysiwyg_include_directory($type) { */ function media_filter($text) { $text = ' ' . $text . ' '; - $text = preg_replace_callback("/\[\[.*?\]\]/s", 'media_token_to_markup', $text); + $text = preg_replace_callback(MEDIA_TOKEN_REGEX, 'media_token_to_markup', $text); return $text; } @@ -106,6 +108,79 @@ function _media_url_curry($func, $arity) { } /** + * Implementation of hook_field_attach_insert(). + * + * Track file usage for media files included in text filter. Note that this + * is heavy-handed, and should be replaced when input filters are + * context-aware. + */ +function media_field_attach_insert($entity_type, $entity) { + _media_filter_parse_from_fields($entity_type, $entity); +} + +/** + * Implementation of hook_field_attach_update(). + * + * @see media_field_attach_insert(). + */ +function media_field_attach_update($entity_type, $entity) { + _media_filter_parse_from_fields($entity_type, $entity); +} + +/** + * Helper function to parse files from media filter and add file usage. + */ +function _media_filter_parse_from_fields($entity_type, $entity) { + $fields = $entity_type == 'node' ? field_info_instances($entity_type, $entity->type) : field_info_instances($entity_type); + $entity_info = entity_get_info($entity_type); + $entity_id = $entity->{$entity_info['entity keys']['id']}; + + // Track the total usage for files from all fields combined. + $usage = array(); + + // Get all of the fields on this entity that allow input filtering. + $fields_with_format = array(); + foreach ($fields as $field_name => $field) { + if (isset($field['settings']['text_processing']) && $field['settings']['text_processing']) { + $fields_with_format[] = $field_name; + } + } + + foreach ($fields_with_format as $field_name) { + $field = field_get_items($entity_type, $entity, $field_name); + foreach ($field as $field_item) { + preg_match_all(MEDIA_TOKEN_REGEX, $field_item['value'], $matches); + foreach ($matches[0] as $tag) { + $tag = str_replace(array('[[', ']]'), '', $tag); + $tag_info = drupal_json_decode($tag); + + if (isset($tag_info['fid'])) { + @$usage[$tag_info['fid']]++; + } + else { + throw new Exception('No file Id'); + } + } + } + } + + // First, clear out all file usage for this entity. This ensure that files + // that are no longer referenced are removed. + db_delete('file_usage') + ->condition('module', 'media') + ->condition('type', $entity_type) + ->condition('id', $entity_id) + ->execute(); + + if (!empty($usage)) { + $files = file_load_multiple(array_keys($usage)); + foreach ($files as $fid => $file) { + file_usage_add($file, 'media', $entity_type, $entity_id, $usage[$fid]); + } + } +} + +/** * Parses the contents of a CSS declaration block and returns a keyed array of property names and values. * * @param $declarations