You are here

public static function FileUrlHandler::urlToFile in File URL 8

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

Get the right file object from an URL.

Parameters

string $url: URL.

Return value

\Drupal\file\FileInterface The file object.

2 calls to FileUrlHandler::urlToFile()
FileUrlFormatter::prepareView in src/Plugin/Field/FieldFormatter/FileUrlFormatter.php
Loads the entities referenced in that field across all the entities being viewed.
FileUrlWidget::value in src/Plugin/Field/FieldWidget/FileUrlWidget.php
Form API callback. Retrieves the value for the file_generic field element.

File

src/FileUrlHandler.php, line 49

Class

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

Namespace

Drupal\file_url

Code

public static function urlToFile($url) {

  // Not a url, but a normal file ID. This can occurs when an object is
  // created in code (not through a form).
  if (is_numeric($url)) {
    return File::load($url);
  }
  $matches = [];
  preg_match('/\\/file-dereference\\/([0-9]+)/', $url, $matches);
  if (!empty($matches)) {
    return File::load($matches[1]);
  }
  return RemoteFile::load($url);
}