file Link.php

Namespace

Drupal\bootstrap\Plugin\Preprocess
  1. <?php
  2. namespace Drupal\bootstrap\Plugin\Preprocess;
  3. use Drupal\bootstrap\Bootstrap;
  4. use Drupal\bootstrap\Utility\Element;
  5. use Drupal\bootstrap\Utility\Variables;
  6. use Drupal\Core\Link;
  7. use Drupal\Core\Url;
  8. use Drupal\file\Entity\File;
  9. /**
  10. * Pre-processes variables for the "file_link" theme hook.
  11. *
  12. * @ingroup plugins_preprocess
  13. *
  14. * @BootstrapPreprocess("file_link",
  15. * replace = "template_preprocess_file_link"
  16. * )
  17. */
  18. class FileLink extends PreprocessBase {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function preprocessVariables(Variables $variables) {
  23. $options = [];
  24. $file = ($variables['file'] instanceof File) ? $variables['file'] : File::load($variables['file']->fid);
  25. $url = file_create_url($file->getFileUri());
  26. $file_size = $file->getSize();
  27. $mime_type = $file->getMimeType();
  28. // Set options as per anchor format described at
  29. // http://microformats.org/wiki/file-format-examples
  30. $options['attributes']['type'] = "$mime_type; length=$file_size";
  31. // Use the description as the link text if available.
  32. if (empty($variables['description'])) {
  33. $link_text = $file->getFilename();
  34. }
  35. else {
  36. $link_text = $variables['description'];
  37. $options['attributes']['title'] = $file->getFilename();
  38. }
  39. // Retrieve the generic mime type from core (mislabeled as "icon_class").
  40. $generic_mime_type = file_icon_class($mime_type);
  41. // Map the generic mime types to an icon and state.
  42. $mime_map = [
  43. 'application-x-executable' => [
  44. 'label' => t('binary file'),
  45. 'icon' => 'console',
  46. ],
  47. 'audio' => [
  48. 'label' => t('audio file'),
  49. 'icon' => 'headphones',
  50. ],
  51. 'image' => [
  52. 'label' => t('image'),
  53. 'icon' => 'picture',
  54. ],
  55. 'package-x-generic' => [
  56. 'label' => t('archive'),
  57. 'icon' => 'compressed',
  58. ],
  59. 'text' => [
  60. 'label' => t('document'),
  61. 'icon' => 'file',
  62. ],
  63. 'video' => [
  64. 'label' => t('video'),
  65. 'icon' => 'film',
  66. ],
  67. ];
  68. // Retrieve the mime map array.
  69. $mime = isset($mime_map[$generic_mime_type]) ? $mime_map[$generic_mime_type] : [
  70. 'label' => t('file'),
  71. 'icon' => 'file',
  72. 'state' => 'primary',
  73. ];
  74. // Classes to add to the file field for icons.
  75. $variables->addClass([
  76. 'file',
  77. // Add a specific class for each and every mime type.
  78. 'file--mime-' . strtr($mime_type, ['/' => '-', '.' => '-']),
  79. // Add a more general class for groups of well known mime types.
  80. 'file--' . $generic_mime_type,
  81. ]);
  82. // Set the icon for the mime type.
  83. $icon = Bootstrap::glyphicon($mime['icon']);
  84. $variables->icon = Element::create($icon)
  85. ->addClass('text-primary')
  86. ->getArray();
  87. $options['attributes']['title'] = t('Open @mime in new window', ['@mime' => $mime['label']]);
  88. $options['attributes']['target'] = '_blank';
  89. if ($this->theme->getSetting('tooltip_enabled')) {
  90. $options['attributes']['data-toggle'] = 'tooltip';
  91. $options['attributes']['data-placement'] = 'bottom';
  92. }
  93. $variables['link'] = Link::fromTextAndUrl($link_text, Url::fromUri($url, $options));
  94. // Add the file size as a variable.
  95. $variables->file_size = format_size($file_size);
  96. // Preprocess attributes.
  97. $this->preprocessAttributes();
  98. }
  99. }

Classes

Name Description
FileLink Pre-processes variables for the "file_link" theme hook.