diff --git a/includes/media.filter.inc b/includes/media.filter.inc index 9e2048b..e63520d 100644 --- a/includes/media.filter.inc +++ b/includes/media.filter.inc @@ -7,6 +7,9 @@ * @TODO: Rename this file? */ +define('MEDIA_TOKEN_REGEX', '/\[\[.*?\]\]/s'); +define('MEDIA_TOKEN_REGEX_ALT', '/%7B.*?%7D/s'); + /** * Implements hook_wysiwyg_include_directory(). */ @@ -26,12 +29,130 @@ 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; } /** + * Implements hook_field_attach_insert(). + * + * Track file usage for media files included in formatted text. Note that this + * is heavy-handed, and should be replaced when Drupal's filter system is + * context-aware. + */ +function media_field_attach_insert($entity_type, $entity) { + _media_filter_add_file_usage_from_fields($entity_type, $entity); +} + +/** + * Implements hook_field_attach_update(). + * + * @see media_field_attach_insert(). + */ +function media_field_attach_update($entity_type, $entity) { + _media_filter_add_file_usage_from_fields($entity_type, $entity); +} + +/** + * Add file usage from file references in an entity's text fields. + */ +function _media_filter_add_file_usage_from_fields($entity_type, $entity) { + // Track the total usage for files from all fields combined. + $usage = array(); + foreach (media_filter_parse_from_fields($entity_type, $entity) as $file_reference) { + if (!isset($usage[$file_reference['fid']])) { + $usage[$file_reference['fid']] = 1; + } + else { + $usage[$file_reference['fid']]++; + } + } + + // First, clear out all file usage for this entity. This ensure that files + // that are no longer referenced are removed. + list($entity_id) = entity_extract_ids($entity_type, $entity); + db_delete('file_usage') + ->condition('module', 'media') + ->condition('type', $entity_type) + ->condition('id', $entity_id) + ->execute(); + + // Add a file usage record for each file reference that was found. + 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]); + } + } +} + +/** + * Parse file references from an entity's text fields and return them as an array. + */ +function media_filter_parse_from_fields($entity_type, $entity) { + $file_references = array(); + + foreach (_media_filter_fields_with_text_filtering($entity_type, $entity) as $field_name) { + if ($field_items = field_get_items($entity_type, $entity, $field_name)) { + foreach ($field_items 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'])) { + $file_references[] = $tag_info; + } + } + + preg_match_all(MEDIA_TOKEN_REGEX_ALT, $field_item['value'], $matches_alt); + foreach ($matches_alt[0] as $tag) { + $tag = urldecode($tag); + $tag_info = drupal_json_decode($tag); + if (isset($tag_info['fid'])) { + $file_references[] = $tag_info; + } + } + } + } + } + + return $file_references; +} + +/** + * Returns an array containing the names of all fields that perform text filtering. + */ +function _media_filter_fields_with_text_filtering($entity_type, $entity) { + list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity); + $fields = field_info_instances($entity_type, $bundle); + + // Get all of the fields on this entity that allow text filtering. + $fields_with_text_filtering = array(); + foreach ($fields as $field_name => $field) { + if (!empty($field['settings']['text_processing'])) { + $fields_with_text_filtering[] = $field_name; + } + } + + return $fields_with_text_filtering; +} + +/** + * Implements hook_entity_delete(). + */ +function media_entity_delete($entity, $type) { + list($entity_id) = entity_extract_ids($type, $entity); + + db_delete('file_usage') + ->condition('module', 'media') + ->condition('type', $type) + ->condition('id', $entity_id) + ->execute(); +} + + +/** * Parses the contents of a CSS declaration block and returns a keyed array of property names and values. * * @param $declarations