diff --git a/core/modules/file/css/file.formatter.generic.css b/core/modules/file/css/file.formatter.generic.css
new file mode 100644
index 0000000..b36310a
--- /dev/null
+++ b/core/modules/file/css/file.formatter.generic.css
@@ -0,0 +1,57 @@
+/**
+ * @file
+ * Default style for file module
+ */
+
+/**
+ * File icons
+ */
+.file {
+ padding-left: 20px;
+ display: inline-block;
+ min-height: 16px;
+ background-repeat: no-repeat;
+ background-position: left center;
+}
+.file.package-x-generic {
+ background-image: url(../icons/package-x-generic.png);
+}
+.file.office-spreadsheet {
+ background-image: url(../icons/x-office-spreadsheet.png);
+}
+.file.office-document {
+ background-image: url(../icons/x-office-document.png);
+}
+.file.office-presentation {
+ background-image: url(../icons/x-office-presentation.png);
+}
+.file.text-x-script {
+ background-image: url(../icons/text-x-script.png);
+}
+.file.text-html {
+ background-image: url(../icons/text-html.png);
+}
+.file.text-plain {
+ background-image: url(../icons/text-plain.png);
+}
+.file.application-pdf {
+ background-image: url(../icons/application-pdf.png);
+}
+.file.application-octet-stream {
+ background-image: url(../icons/application-octet-stream.png);
+}
+.file.application-x-executable {
+ background-image: url(../icons/application-x-executable.png);
+}
+.file.audio {
+ background-image: url(../icons/audio-x-generic.png);
+}
+.file.video {
+ background-image: url(../icons/video-x-generic.png);
+}
+.file.text {
+ background-image: url(../icons/text-x-generic.png);
+}
+.file.image {
+ background-image: url(../icons/image-x-generic.png);
+}
diff --git a/core/modules/file/file.libraries.yml b/core/modules/file/file.libraries.yml
index ccc0c3b..4652958 100644
--- a/core/modules/file/file.libraries.yml
+++ b/core/modules/file/file.libraries.yml
@@ -5,8 +5,16 @@ drupal.file:
css:
theme:
css/file.admin.css: {}
+ component:
+ css/file.formatter.generic.css: {}
dependencies:
- core/jquery
- core/jquery.once
- core/drupal
- core/drupalSettings
+
+drupal.file.formatter.generic:
+ version: VERSION
+ css:
+ theme:
+ css/file.formatter.generic.css: {}
diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index e17ab46..e78afe2 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -576,7 +576,7 @@ function file_theme() {
return array(
// From file.module.
'file_link' => array(
- 'variables' => array('file' => NULL, 'icon_directory' => NULL, 'description' => NULL, 'attributes' => array()),
+ 'variables' => array('file' => NULL, 'description' => NULL, 'attributes' => array()),
'template' => 'file-link',
),
'file_managed_file' => array(
@@ -1542,13 +1542,10 @@ function file_managed_file_pre_render($element) {
*/
function template_preprocess_file_link(&$variables) {
$file = $variables['file'];
- $options = array(
- 'attributes' => $variables['attributes'],
- );
- $icon_directory = $variables['icon_directory'];
+ $options = array();
- $url = file_create_url($file->getFileUri());
$file_entity = ($file instanceof File) ? $file : file_load($file->fid);
+ $url = file_create_url($file_entity->getFileUri());
// Human-readable names, for use as text-alternatives to icons.
$mime_name = array(
@@ -1568,14 +1565,6 @@ function template_preprocess_file_link(&$variables) {
'application/octet-stream' => t('Binary Data'),
);
- $variables['icon'] = array(
- '#theme' => 'image__file_icon',
- '#uri' => file_icon_url($file_entity, $icon_directory),
- '#alt' => (!empty($mime_name[$file->getMimeType()])) ? $mime_name[$file->getMimeType()] : t('File'),
- '#title' => String::checkPlain($file_entity->getFilename()),
- '#attributes' => array('class' => array('file-icon')),
- );
-
// Set options as per anchor format described at
// http://microformats.org/wiki/file-format-examples
$options['attributes']['type'] = $file->getMimeType() . '; length=' . $file->getSize();
@@ -1589,80 +1578,50 @@ function template_preprocess_file_link(&$variables) {
$options['attributes']['title'] = String::checkPlain($file_entity->getFilename());
}
- $variables['link'] = l($link_text, $url, $options);
- $variables['attributes'] = array('class' => array('file'));
-}
-
-/**
- * Creates a URL to the icon for a file entity.
- *
- * @param \Drupal\file\FileInterface $file
- * A file entity.
- * @param $icon_directory
- * (optional) A path to a directory of icons to be used for files. Defaults to
- * the value of the "icon.directory" variable.
- *
- * @return
- * A URL string to the icon, or FALSE if an appropriate icon cannot be found.
- */
-function file_icon_url(FileInterface $file, $icon_directory = NULL) {
- if ($icon_path = file_icon_path($file, $icon_directory)) {
- return base_path() . $icon_path;
+ // Set file classes to the options array.
+ if (isset($variables['attributes']['class']) && is_array($variables['attributes']['class'])) {
+ $variables['attributes']['class'] = array_merge($variables['attributes']['class'], file_icon_classes($file));
}
- return FALSE;
+ else {
+ $variables['attributes']['class'] = file_icon_classes($file);
+ }
+
+ $variables['link'] = l($link_text, $url, $options);
}
/**
- * Creates a path to the icon for a file entity.
+ * Creates a list of classes to the icon for a file entity.
*
- * @param \Drupal\file\FileInterface $file
+ * @param \Drupal\file\File $file
* A file entity.
- * @param $icon_directory
- * (optional) A path to a directory of icons to be used for files. Defaults to
- * the value of the "icon.directory" variable.
*
* @return
- * A string to the icon as a local path, or FALSE if an appropriate icon could
- * not be found.
+ * A array of the classes associated to the file.
*/
-function file_icon_path(FileInterface $file, $icon_directory = NULL) {
- // Use the default set of icons if none specified.
- if (!isset($icon_directory)) {
- $icon_directory = \Drupal::config('file.settings')->get('icon.directory');
- }
-
- // If there's an icon matching the exact mimetype, go for it.
- $dashed_mime = strtr($file->getMimeType(), array('/' => '-'));
- $icon_path = $icon_directory . '/' . $dashed_mime . '.png';
- if (file_exists($icon_path)) {
- return $icon_path;
- }
+function file_icon_classes(File $file) {
+ $classes = array('file');
- // For a few mimetypes, we can "manually" map to a generic icon.
+ // Search for a group with the files MIME type.
$generic_mime = (string) file_icon_map($file);
- $icon_path = $icon_directory . '/' . $generic_mime . '.png';
- if ($generic_mime && file_exists($icon_path)) {
- return $icon_path;
+ if (!empty($generic_mime)) {
+ $classes[] = $generic_mime;
}
// Use generic icons for each category that provides such icons.
foreach (array('audio', 'image', 'text', 'video') as $category) {
- if (strpos($file->getMimeType(), $category . '/') === 0) {
- $icon_path = $icon_directory . '/' . $category . '-x-generic.png';
- if (file_exists($icon_path)) {
- return $icon_path;
- }
+ if (strpos($file->getMimeType(), $category) === 0) {
+ $classes[] = $category;
}
}
- // Try application-octet-stream as last fallback.
- $icon_path = $icon_directory . '/application-octet-stream.png';
- if (file_exists($icon_path)) {
- return $icon_path;
+ // If there's no generic icon for the type use a specific one.
+ if (count($classes) == 1) {
+ $dashed_mime = strtr($file->getMimeType(), array('/' => '-'));
+ $classes[] = $dashed_mime;
+ $classes[] = 'file-general';
}
- // No icon can be found.
- return FALSE;
+ return $classes;
}
/**
@@ -1693,7 +1652,7 @@ function file_icon_map(FileInterface $file) {
case 'application/x-applix-word':
case 'application/x-kword':
case 'application/x-kword-crypt':
- return 'x-office-document';
+ return 'office-document';
// Spreadsheet document types.
case 'application/vnd.ms-excel':
@@ -1709,7 +1668,7 @@ function file_icon_map(FileInterface $file) {
case 'application/x-gnumeric':
case 'application/x-kspread':
case 'application/x-kspread-crypt':
- return 'x-office-spreadsheet';
+ return 'office-spreadsheet';
// Presentation document types.
case 'application/vnd.ms-powerpoint':
@@ -1721,7 +1680,7 @@ function file_icon_map(FileInterface $file) {
case 'application/vnd.sun.xml.impress':
case 'application/vnd.sun.xml.impress.template':
case 'application/x-kpresenter':
- return 'x-office-presentation';
+ return 'office-presentation';
// Compressed archive types.
case 'application/zip':
diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php
index 5d137e0..e37633b 100644
--- a/core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php
+++ b/core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php
@@ -46,6 +46,12 @@ public function viewElements(FieldItemListInterface $items) {
}
}
+ if (!empty($elements)) {
+ $elements['#attached'] = array(
+ 'library' => array('file/drupal.file.formatter.generic'),
+ );
+ }
+
return $elements;
}
diff --git a/core/modules/file/templates/file-link.html.twig b/core/modules/file/templates/file-link.html.twig
index fbbf08b..44cbd7f 100644
--- a/core/modules/file/templates/file-link.html.twig
+++ b/core/modules/file/templates/file-link.html.twig
@@ -6,11 +6,10 @@
* Available variables:
* - attributes: The HTML attributes for the containing element.
* - link: A link to the file.
- * - icon: The icon image representing the file type.
*
* @see template_preprocess_file_link()
*
* @ingroup themeable
*/
#}
-{{ icon }} {{ link }}
+{{ link }}