diff --git a/file_entity.module b/file_entity.module index 8989699b8b6d6d7da4797bc3ad7da587231c87b9..75ef771ce675cb56b31b18823f9073210a80af9f 100644 --- a/file_entity.module +++ b/file_entity.module @@ -168,6 +168,16 @@ function file_entity_menu() { 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10, ); + $items['file/%file/usage'] = array( + 'title' => 'Usage', + 'page callback' => 'file_entity_usage_page', + 'page arguments' => array(1), + 'access callback' => 'file_entity_access', + 'access arguments' => array('update', 1), + 'type' => MENU_LOCAL_TASK, + 'context' => MENU_CONTEXT_PAGE, + 'file' => 'file_entity.pages.inc', + ); $items['file/%file/edit'] = array( 'title' => 'Edit', 'page callback' => 'drupal_get_form', @@ -391,6 +401,7 @@ function file_entity_admin_paths() { 'file/add' => TRUE, 'file/add/*' => TRUE, 'file/*/edit' => TRUE, + 'file/*/usage' => TRUE, 'file/*/delete' => TRUE, ); return $paths; @@ -416,7 +427,7 @@ function file_entity_theme() { 'file_entity_file_link' => array( 'variables' => array('file' => NULL, 'icon_directory' => NULL), 'file' => 'file_entity.theme.inc', - ) + ), ); } diff --git a/file_entity.pages.inc b/file_entity.pages.inc index 5fb1fc2a56b0d6a26475de78e01426b94afa2cc7..57f91d0a950a5a2ef9410591a714e7a2627dac9f 100644 --- a/file_entity.pages.inc +++ b/file_entity.pages.inc @@ -57,6 +57,64 @@ function file_entity_add_upload($form, &$form_state, array $options = array()) { return $form; } +/** + * Page callback to show file usage information. + */ +function file_entity_usage_page($file) { + $rows = array(); + $occured_entities = array(); + + foreach (file_usage_list($file) as $module => $usage) { + $info = system_get_info('module', $module); + + // There are cases, where actual entitiy doesen't exist. We have to handle this. + foreach ($usage as $entity_type => $entity_ids) { + $entity_info = entity_get_info($entity_type); + $entities = empty($entity_info) ? NULL : entity_load($entity_type, array_keys($entity_ids)); + + foreach ($entity_ids as $entity_id => $count) { + // If some other module already added this entity just sum all counts. + if (isset($occured_entities[$entity_type][$entity_id])) { + $rows[$occured_entities[$entity_type][$entity_id]][2] += $count; + continue; + } + + $label = empty($entities[$entity_id]) ? $module : entity_label($entity_type, $entities[$entity_id]); + $entity_uri = empty($entities[$entity_id]) ? NULL : entity_uri($entity_type, $entities[$entity_id]); + + // Some entities do not have URL. + if (empty($entity_uri)) { + $rows[] = array($entity_type, check_plain($label), $count); + } + else { + $uri = $entity_uri['path']; + $rows[] = array($entity_type, l($label, $uri), $count); + } + + $occured_entities[$entity_type][$entity_id] = count($rows) - 1; + } + } + } + $header[] = array( + 'data' => t('Type'), + ); + $header[] = array( + 'data' => t('Title'), + ); + $header[] = array( + 'data' => t('Count'), + ); + $build['usage_table'] = array( + '#theme' => 'table', + '#header' => $header, + '#rows' => $rows, + '#caption' => t('This table lists all of the places where @filename is used.', + array('@filename' => $file->filename)), + '#empty' => t('This file is not currently used.'), + ); + return $build; +} + /** * Upload a file. */