function theme_file_link

Returns HTML for a link to a file.

Parameters

$variables: An associative array containing:

  • file: A file object to which the link will be created.
  • icon_directory: (optional) A path to a directory of icons to be used for files. Defaults to the value of the "icon.directory" variable.

Related topics

4 theme calls to theme_file_link()
FileFieldDisplayTest::testNodeDisplay in drupal/core/modules/file/lib/Drupal/file/Tests/FileFieldDisplayTest.php
Tests normal formatter display on node display.
file_managed_file_process in drupal/core/modules/file/file.module
Render API callback: Expands the managed_file element type.
GenericFileFormatter::viewElements in drupal/core/modules/file/lib/Drupal/file/Plugin/field/formatter/GenericFileFormatter.php
Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
theme_file_formatter_table in drupal/core/modules/file/file.field.inc
Returns HTML for a file attachments table.

File

drupal/core/modules/file/file.module, line 1239
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

function theme_file_link($variables) {
  $file = $variables['file'];
  $icon_directory = $variables['icon_directory'];
  $url = file_create_url($file->uri);

  // theme_file_icon() requires a file entity, make sure it gets one.
  $icon = theme('file_icon', array(
    'file' => $file instanceof File ? $file : file_load($file->fid),
    'icon_directory' => $icon_directory,
  ));

  // Set options as per anchor format described at
  // http://microformats.org/wiki/file-format-examples
  $options = array(
    'attributes' => array(
      'type' => $file->filemime . '; length=' . $file->filesize,
    ),
  );

  // Use the description as the link text if available.
  if (empty($file->description)) {
    $link_text = $file->filename;
  }
  else {
    $link_text = $file->description;
    $options['attributes']['title'] = check_plain($file->filename);
  }
  return '<span class="file">' . $icon . ' ' . l($link_text, $url, $options) . '</span>';
}