function template_preprocess_file in File Entity (fieldable files) 8.2
Prepares variables for file templates.
Default template: file.html.twig.
Parameters
array $variables: An associative array containing:
- elements: An array of elements to display in view mode.
- file: The file object.
File
- ./
file_entity.module, line 221 - Extends Drupal file entities to be fieldable and viewable.
Code
function template_preprocess_file(&$variables) {
$view_mode = $variables['view_mode'] = $variables['elements']['#view_mode'];
$variables['file'] = $variables['elements']['#file'];
/** @var FileInterface $file */
$file = $variables['file'];
$variables['id'] = $file
->id();
$variables['date'] = \Drupal::service('date.formatter')
->format($file
->getCreatedTime());
$username = array(
'#theme' => 'username',
'#account' => $file
->getOwner(),
'#link_options' => array(
'attributes' => array(
'rel' => 'author',
),
),
);
$variables['name'] = $username;
$variables['file_url'] = $file
->toUrl('canonical')
->toString();
$variables['label'] = $file
->label();
$variables['page'] = $view_mode == 'full' && $file
->isPage();
// Hide the file name from being displayed until we can figure out a better
// way to control this. We cannot simply not output the title since
// contextual links require $title_suffix to be output in the template.
// @see http://drupal.org/node/1245266
if (!$variables['page']) {
$variables['title_attributes_array']['class'][] = 'element-invisible';
}
// Helpful $content variable for templates.
$variables += array(
'content' => array(),
);
foreach (\Drupal\Core\Render\Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
// Attach the file object to the content element.
$variables['content']['file']['#file'] = $file;
}