Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The function template_preprocess_file_link()
in core/modules/file/file.module
starts out like this:
function template_preprocess_file_link(&$variables) {
$file = $variables['file'];
$options = [];
$file_entity = ($file instanceof File) ? $file : File::load($file->fid);
// @todo Wrap in file_url_transform_relative(). This is currently
// impossible. As a work-around, we currently add the 'url.site' cache context
// to ensure different file URLs are generated for different sites in a
// multisite setup, including HTTP and HTTPS versions of the same site.
// Fix in https://www.drupal.org/node/2646744.
$url = $file_entity->createFileUrl(FALSE);
$variables['#cache']['contexts'][] = 'url.site';
$mime_type = $file->getMimeType();
// Set options as per anchor format described at
// http://microformats.org/wiki/file-format-examples
$options['attributes']['type'] = $mime_type . '; length=' . $file->getSize();
Note how it tries to be backward-compatible with the passing of a stdClass object in $variables['file']
by calling File::load()
to load a File object. However, these lines will cause an exception:
$mime_type = $file->getMimeType();
$options['attributes']['type'] = $mime_type . '; length=' . $file->getSize();
because they are using $file
, not $file_entity
.
These lines should either be fixed to use $file_entity
, or support for passing a stdClass object should be completely removed.
Comment | File | Size | Author |
---|---|---|---|
#4 | 3055474.patch | 1.89 KB | Gribnif |
Comments
Comment #2
Gribnif CreditAttribution: Gribnif commentedComment #3
Berdir> or support for passing a stdClass object should be completely removed.
I'd vote for that. Since it already fatals, I don't think that is an API change, should be a fairly simple patch then.
Comment #4
Gribnif CreditAttribution: Gribnif commentedHere's a patch.
Comment #6
BerdirThanks, I don't think this is need a change record or anything, it was a fatal error before, so it didn't work, in that sense, it's just a bit of code cleanup.
Comment #7
alexpottYeah we probably broke this somewhere along the way - perhaps in #2321969: Replace all instances of file_load(), file_load_multiple(), entity_load('file') and entity_load_multiple('file') with static method calls.
Committed and pushed 070e49b1a5 to 8.8.x and e480787030 to 8.7.x. Thanks!