You are here

function printable_file_url_alter in Printer and PDF versions for Drupal 8+ 8

Same name and namespace in other branches
  1. 2.x printable.module \printable_file_url_alter()

Make file urls absolute if we're generating a PDF.

Parameters

string $uri: URI to be altered.

File

./printable.module, line 271
Provides printer friendly content entities.

Code

function printable_file_url_alter(&$uri) {
  if (printable_preparing_pdf()) {

    // Using the filesystem path makes images work across all libraries but
    // won't work with paths that require Drupal foo. I'd prefer to use URLs
    // but that doesn't work with all libraries :(.
    global $base_path;
    $scheme = \Drupal::service('file_system')
      ->uriScheme($uri);

    // If the file uses the public scheme,
    if ($scheme == 'public') {
      $path = \Drupal::service('file_system')
        ->realpath('public://');
      $uri = $path . '/' . substr($uri, 9);
    }
    else {
      $base_url = \Drupal::request()
        ->getSchemeAndHttpHost();
      if (substr($uri, 0, strlen($base_url)) == $base_url) {
        $uri = substr($uri, strlen($base_url) + 1);
      }
      $uri = DRUPAL_ROOT . '/' . $uri;
    }
  }
}