function file_uri_to_object in File Entity (fieldable files) 8.2
Same name and namespace in other branches
- 7.3 file_entity.file_api.inc \file_uri_to_object()
- 7 file_entity.file_api.inc \file_uri_to_object()
- 7.2 file_entity.file_api.inc \file_uri_to_object()
Returns a file object which can be passed to file_save().
@todo This should probably be named file_load_by_uri($uri, $create_if_not_exists). @todo Remove this function when http://drupal.org/node/685818 is fixed.
Parameters
string $uri: A string containing the URI, path, or filename.
bool $use_existing: (Optional) If TRUE and there's an existing file in the {file_managed} table with the passed in URI, then that file object is returned. Otherwise, a new file object is returned. Default is TRUE.
Return value
FileInterface|bool A file object, or FALSE on error.
1 call to file_uri_to_object()
- file_entity_file_download in ./
file_entity.module - Implements hook_file_download().
File
- ./
file_entity.module, line 465 - Extends Drupal file entities to be fieldable and viewable.
Code
function file_uri_to_object($uri, $use_existing = TRUE) {
$file = FALSE;
$uri = \Drupal::service('stream_wrapper_manager')
->normalizeUri($uri);
if ($use_existing) {
// We should always attempt to re-use a file if possible.
$files = \Drupal::entityTypeManager()
->getStorage('file')
->loadByProperties([
'uri' => $uri,
]);
$file = !empty($files) ? reset($files) : FALSE;
}
if (empty($file)) {
$file = File::create(array(
'uid' => \Drupal::currentUser()
->id(),
'uri' => $uri,
'status' => FILE_STATUS_PERMANENT,
));
}
return $file;
}