summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorParis Liakos2012-11-11 03:17:54 (GMT)
committer Paris Liakos2012-11-11 03:17:54 (GMT)
commit11894c17896f9fd7431cf780c245e1e6d63f837a (patch)
tree5c273916cdc7c7a448cd62acb4b69d9063f02268
parent945bf90b283ef56a48bdb5a7597ac7cfcbf74809 (diff)
Issue #1286508 by Dave Reid, mtift, Pomliane, slashrsm, gooddesignusa: Provide a file usage report tab at file/%file/usage
-rw-r--r--file_entity.module13
-rw-r--r--file_entity.pages.inc58
2 files changed, 70 insertions, 1 deletions
diff --git a/file_entity.module b/file_entity.module
index 8989699..75ef771 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 5fb1fc2..57f91d0 100644
--- a/file_entity.pages.inc
+++ b/file_entity.pages.inc
@@ -58,6 +58,64 @@ function file_entity_add_upload($form, &$form_state, array $options = array()) {
}
/**
+ * 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.
*/
function file_entity_add_upload_submit($form, &$form_state) {