You are here

function template_preprocess_file_entity in File Entity (fieldable files) 7

Same name and namespace in other branches
  1. 7.3 file_entity.module \template_preprocess_file_entity()
  2. 7.2 file_entity.module \template_preprocess_file_entity()

Process variables for file_entity.tpl.php

The $variables array contains the following arguments:

  • $file
  • $view_mode

See also

file_entity.tpl.php

File

./file_entity.module, line 530
Extends Drupal file entities to be fieldable and viewable.

Code

function template_preprocess_file_entity(&$variables) {
  $view_mode = $variables['view_mode'] = $variables['elements']['#view_mode'];
  $variables['file'] = $variables['elements']['#file'];
  $file = $variables['file'];
  $variables['date'] = format_date($file->timestamp);
  $account = user_load($file->uid);
  $variables['name'] = theme('username', array(
    'account' => $account,
  ));

  // @todo Use entity_uri once http://drupal.org/node/1057242 is fixed.

  //$uri = entity_uri('file', $file);

  //$variables['file_url']  = url($uri['path'], $uri['options']);
  $variables['file_url'] = file_create_url($file->uri);
  $label = entity_label('file', $file);
  $variables['label'] = check_plain($label);
  $variables['page'] = $view_mode == 'full' && file_is_page($file);

  // Disable the file name from being displayed as the title until we can
  // figure out a better way to control this.
  // @see http://drupal.org/node/1245266
  $variables['page'] = TRUE;

  // Flatten the file object's member fields.
  $variables = array_merge((array) $file, $variables);

  // Helpful $content variable for templates.
  $variables += array(
    'content' => array(),
  );
  foreach (element_children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }

  // Make the field variables available with the appropriate language.
  field_attach_preprocess('file', $file, $variables['content'], $variables);

  // Display post information only on certain file types.
  if (variable_get('file_submitted_' . $file->type, FALSE)) {
    $variables['display_submitted'] = TRUE;
    $variables['submitted'] = t('Uploaded by !username on !datetime', array(
      '!username' => $variables['name'],
      '!datetime' => $variables['date'],
    ));
    $variables['user_picture'] = theme_get_setting('toggle_file_user_picture') ? theme('user_picture', array(
      'account' => $account,
    )) : '';
  }
  else {
    $variables['display_submitted'] = FALSE;
    $variables['submitted'] = '';
    $variables['user_picture'] = '';
  }

  // Gather file classes.
  $variables['classes_array'][] = drupal_html_class('file-' . $file->type);
  $variables['classes_array'][] = drupal_html_class('file-' . $file->filemime);
  if ($file->status != FILE_STATUS_PERMANENT) {
    $variables['classes_array'][] = 'file-temporary';
  }

  // Change the 'file-entity' class into 'file'
  if ($variables['classes_array'][0] == 'file-entity') {
    $variables['classes_array'][0] = 'file';
  }

  // Clean up name so there are no underscores.
  $variables['theme_hook_suggestions'][] = 'file__' . $file->type;
  $variables['theme_hook_suggestions'][] = 'file__' . $file->type . '__' . $view_mode;
  $variables['theme_hook_suggestions'][] = 'file__' . str_replace(array(
    '/',
    '-',
  ), array(
    '__',
    '_',
  ), $file->filemime);
  $variables['theme_hook_suggestions'][] = 'file__' . str_replace(array(
    '/',
    '-',
  ), array(
    '__',
    '_',
  ), $file->filemime) . '__' . $view_mode;
  $variables['theme_hook_suggestions'][] = 'file__' . $file->fid;
  $variables['theme_hook_suggestions'][] = 'file__' . $file->fid . '__' . $view_mode;
}