You are here

public static function FileUrlHandler::fileToUrl in File URL 8

Same name and namespace in other branches
  1. 2.0.x src/FileUrlHandler.php \Drupal\file_url\FileUrlHandler::fileToUrl()

Get public dereferenceable URL from file.

Parameters

\Drupal\file\FileInterface $file: File.

Return value

string URL.

Throws

\Exception When a RemoteFile has been accidentally passed.

1 call to FileUrlHandler::fileToUrl()
FileUrlWidget::massageFormValues in src/Plugin/Field/FieldWidget/FileUrlWidget.php
Massages the form values into the format expected for field values.

File

src/FileUrlHandler.php, line 26

Class

FileUrlHandler
Helper class for turning files into public URLs and back.

Namespace

Drupal\file_url

Code

public static function fileToUrl(FileInterface $file) {
  global $base_url;
  if ($file instanceof RemoteFile) {
    throw new \Exception('Only regular files can be converted.');
  }
  $settings = \Drupal::configFactory()
    ->get('file_url.settings');
  $host = rtrim($settings
    ->get('dereference_host'));
  $host = $host ?: $base_url;

  // @see file_url.routing.yml for dereference redirect.
  return $host . '/file-dereference/' . $file
    ->id();
}