You are here

function file_entity_usage_page in File Entity (fieldable files) 7.3

Same name and namespace in other branches
  1. 7.2 file_entity.pages.inc \file_entity_usage_page()

Page callback to show file usage information.

1 string reference to 'file_entity_usage_page'
file_entity_menu in ./file_entity.module
Implements hook_menu().

File

./file_entity.pages.inc, line 366
Supports file operations including View, Edit, and Delete.

Code

function file_entity_usage_page($file) {
  drupal_set_title(t('<em>Usage of @type</em> @title', array(
    '@type' => $file->type,
    '@title' => $file->filename,
  )), PASS_THROUGH);
  $rows = array();
  $occured_entities = array();

  // Determine all of the locations where a file is used, then loop through the
  // occurrences and filter out any duplicates.
  foreach (file_usage_list($file) as $type) {
    foreach ($type as $entity_type => $entity_ids) {

      // There are cases where the actual entity doesn't exist.
      // We have to handle this.
      $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 this entity has already been listed in the table, just add any
        // additional usage to the total count column in the table row and
        // continue on to the next iteration of the loop.
        if (isset($occured_entities[$entity_type][$entity_id])) {
          $rows[$occured_entities[$entity_type][$entity_id]][3] += $count;
          continue;
        }

        // Retrieve the label and the URI of the entity.
        $label = empty($entities[$entity_id]) ? t('(entity not loaded)') : entity_label($entity_type, $entities[$entity_id]);
        if (empty($label)) {
          $label = t('(entity label not loaded)');
        }
        $entity_label = $label;
        $entity_uri = empty($entities[$entity_id]) ? NULL : entity_uri($entity_type, $entities[$entity_id]);

        // Link the label to the URI when possible.
        if (!empty($entity_uri['path']) && $entity_type != 'paragraphs_item') {
          if (empty($entity_uri['options'])) {
            $entity_label = l($label, $entity_uri['path']);
          }
          else {
            $entity_label = l($label, $entity_uri['path'], $entity_uri['options']);
          }
        }
        elseif ($entity_type == 'paragraphs_item') {
          $paragraph_fields = field_read_fields(array(
            'type' => 'paragraphs',
          ));
          foreach ($paragraph_fields as $paragraph_field) {
            $field_name = $paragraph_field['field_name'];
            $query = new EntityFieldQuery();
            $query
              ->entityCondition('entity_type', 'node')
              ->fieldCondition($field_name, 'value', $entity_id, '=');
            $nid = $query
              ->execute();
            if (!empty($nid)) {
              $nid = implode(array_keys($nid['node']));
              if ($nid) {
                $node = node_load($nid);
                $entity_label = l($node->title, 'node/' . $nid);
              }
            }
          }
        }
        else {
          $entity_label = check_plain($label);
        }
        $rows[] = array(
          $entity_label,
          $entity_id,
          $entity_type,
          $count,
        );

        // Record the occurrence of the entity to ensure that it isn't listed in
        // the table again.
        $occured_entities[$entity_type][$entity_id] = count($rows) - 1;
      }
    }
  }
  $header = array(
    t('Entity label'),
    t('Entity ID'),
    t('Entity type'),
    t('Times this file used by this entity'),
  );
  $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;
}