You are here

public function Common::iconGenerate in Filebrowser 3.x

Same name and namespace in other branches
  1. 8.2 src/Services/Common.php \Drupal\filebrowser\Services\Common::iconGenerate()

You can override the icons used by providing your own in theme/active_theme/filebrowser

Create a thumbnail and the associated XHTML code for a specific file.

Parameters

string $file_type. directory, file.:

string $file_mimetype. File mimetype.:

int $height:

int $width:

boolean $return_image. True if you want the function to return: a themed image. If false the function will return only the uri of the .svg file. This is mostly for use in the icon_view display where we need to scale the thumbnails to match the image (grid) dimensions.

Return value

mixed array|string

File

src/Services/Common.php, line 135

Class

Common
Class Common @package Drupal\filebrowser\Services

Namespace

Drupal\filebrowser\Services

Code

public function iconGenerate($file_type, $file_mimetype, $height, $width, $return_image = true) {

  // todo: We can delete the png logic because we use svg
  // todo: abstract this function to be independent of supplied array
  $ext = '.svg';
  $mime_type = $this
    ->mimeIcon($file_mimetype);
  $main_type = dirname($file_type);
  if ($file_type == 'dir' && $mime_type != 'folder-parent') {
    $mime_type = 'directory';
  }
  $theme_path = \Drupal::theme()
    ->getActiveTheme()
    ->getPath() . "/filebrowser/icons/";
  $icons = [
    // search first in active theme
    $theme_path . $mime_type . $ext,
    $theme_path . $main_type . $ext,
    // use default filebrowser icons
    $this
      ->filebrowserPath() . '//icons/' . $mime_type . $ext,
    $this
      ->filebrowserPath() . '//icons/' . $main_type . $ext,
  ];
  $eligible = $this
    ->filebrowserPath() . '//icons/' . 'unknown' . $ext;
  foreach ($icons as $icon) {
    if (file_exists($icon)) {
      $eligible = $icon;
      break;
    }
  }

  // todo:
  // We are adding the CSS classes to Twig using variable data.class
  // The normal way, using #attributes is not working: investigate & correct
  if ($return_image) {
    $markup = file_get_contents($eligible, \FILE_TEXT);
    return [
      '#theme' => 'filebrowser_icon_svg',
      '#html' => $markup,
      '#data' => [
        'height' => $height,
        'width' => $width,
        'class' => [
          'filebrowser-svg',
          $mime_type . '-icon',
        ],
      ],
      '#test' => 'dir is een test',
    ];
  }
  else {
    return $eligible;
  }
}