append_file_info-8.x-1.0-rc3/append_file_info.module

append_file_info.module
<?php

use Drupal\Core\Link;
/**
 * @file
 * Adds file info (file type icon, extension, size) to local managed file links.
 *
 * This module offers a filter as well as a theme override.
 */

use Drupal\file\Entity\File;
use Drupal\Core\Url;
use Drupal\Core\Template\Attribute;
use Drupal\Component\Utility\Html;

/**
 * Implements hook_theme_registry_alter().
 */
function append_file_info_theme_registry_alter(&$theme_registry) {
  foreach ($theme_registry['file_link']['preprocess functions'] as $key => $value) {
    if ($value == 'template_preprocess_file_link') {
      $theme_registry['file_link']['preprocess functions'][$key] = '_append_file_info_preprocess_file_link';
    }
  }
}

/**
 * Adds one line of custom code (appends file info to $link_text).
 *
 * @see template_preprocess_file_link
 */
function _append_file_info_preprocess_file_link(&$variables) {
  $file = $variables['file'];
  $options = [];

  $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 = \Drupal::service('file_url_generator')->generateAbsoluteString($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 = [
    'file',
    // Add a specific class for each and every mime type.
    'file--mime-' . strtr($mime_type, ['/' => '-', '.' => '-']),
    // 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);
  // Begin custom code.
  if (function_exists('_append_file_info_get_extra_link_text')) {
    $link_text .= _append_file_info_get_extra_link_text($file);
  }
  // End custom code.
  $variables['link'] = Link::fromTextAndUrl($link_text, Url::fromUri($url, $options));
}

/**
 * Gets text to be appended to file links (extension, file size).
 *
 * @param object $file
 *   The file entity object.
 *
 * @return string
 *   The text to append.
 */
function _append_file_info_get_extra_link_text($file) {
  $file_parts = explode('.', \Drupal::service('file_system')->basename($file->getFileUri()));
  $extension = strtoupper(array_pop($file_parts));
  // Add support for TAR.GZ:
  if (in_array($extension, ['GZ', 'BZ2']) && count($file_parts) > 2) {
    $extension = strtoupper(array_pop($file_parts)) . '.' . $extension;
  }
  $link_text = ' (' . Html::escape($extension) . ', ' . format_size($file->getSize()) . ')';
  return $link_text;
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc