template_preprocess_file_link(&$variables)
Prepares variables for file link templates.
Default template: file-link.html.twig.
array $variables: An associative array containing:
function template_preprocess_file_link(&$variables) { $file = $variables['file']; $options = array(); $file_entity = ($file instanceof File) ? $file : File::load($file->fid); // @todo Wrap in file_url_transform_relative(). This is currently // impossible. As a work-around, we currently add the 'url.site' cache context // to ensure different file URLs are generated for different sites in a // multisite setup, including HTTP and HTTPS versions of the same site. // Fix in https://www.drupal.org/node/2646744. $url = file_create_url($file_entity->getFileUri()); $variables['#cache']['contexts'][] = 'url.site'; $mime_type = $file->getMimeType(); // Set options as per anchor format described at // http://microformats.org/wiki/file-format-examples $options['attributes']['type'] = $mime_type . '; length=' . $file->getSize(); // Use the description as the link text if available. if (empty($variables['description'])) { $link_text = $file_entity->getFilename(); } else { $link_text = $variables['description']; $options['attributes']['title'] = $file_entity->getFilename(); } // 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), ); // Set file classes to the options array. $variables['attributes'] = new Attribute($variables['attributes']); $variables['attributes']->addClass($classes); $variables['link'] = \Drupal::l($link_text, Url::fromUri($url, $options)); }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!file!file.module/function/template_preprocess_file_link/8.1.x