You are here

function template_preprocess_track_da_files_file_link in Track da files 8

Prepares variables for file link templates.

Default template: file-link.html.twig.

Parameters

array $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.
  • description: A description to be displayed instead of the filename.
  • attributes: An associative array of attributes to be placed in the a tag.

File

./track_da_files.module, line 268
This file contains Track da files main functions.

Code

function template_preprocess_track_da_files_file_link(&$variables) {
  $file = $variables['file'];
  $options = array();
  $url = file_create_url($file->uri);
  $mime_type = $file
    ->getMimeType();
  $filesize = $file
    ->getSize();
  $filename = $file
    ->getFilename();

  // Set options as per anchor format described at
  // http://microformats.org/wiki/file-format-examples
  $options['attributes']['type'] = $mime_type . '; length=' . $filesize;

  // Use the description as the link text if available.
  if (empty($variables['description'])) {
    $link_text = $filename;
  }
  else {
    $link_text = $variables['description'];
    $options['attributes']['title'] = SafeMarkup::checkPlain($filename);
  }

  // Classes to add to the file field for icons.
  $classes = array(
    'file',
    // Add a specific class for each and every mime type.
    'file--mime-' . strtr($mime_type, array(
      '/' => '-',
      '.' => '-',
    )),
    // Add a more general class for groups of well known mime types.
    'file--' . file_icon_class($mime_type),
  );
  $options['query']['file'] = '1';
  if (isset($variables['entity_bundle'])) {
    $options['query']['type'] = $variables['entity_bundle'];
  }
  if (isset($variables['entity_id'])) {
    $options['query']['id'] = $variables['entity_id'];
  }

  // Set file classes to the options array.
  $variables['attributes'] = new Attribute($variables['attributes']);
  $variables['attributes']
    ->addClass($classes);
  $url = track_da_files_create_url($file
    ->getFileUri());
  $link = \Drupal::l($text, Url::fromUri($url, $options));
  $variables['link'] = $link;
}