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.

CommentFileSizeAuthor
#4 3055474.patch1.89 KBGribnif
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Gribnif created an issue. See original summary.

Gribnif’s picture

Title: template_preprocess_file_link will not work with a StdObject, though it tries to » template_preprocess_file_link will not work with a stdClass object, though it tries to
Berdir’s picture

Component: file system » file.module
Issue tags: +Novice

> 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.

Gribnif’s picture

Status: Active » Needs review
FileSize
1.89 KB

Here's a patch.

Status: Needs review » Needs work

The last submitted patch, 4: 3055474.patch, failed testing. View results

Berdir’s picture

Status: Needs work » Reviewed & tested by the community

Thanks, 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.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Yeah 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!

  • alexpott committed 070e49b on 8.8.x
    Issue #3055474 by Gribnif, Berdir: template_preprocess_file_link will...

  • alexpott committed e480787 on 8.7.x
    Issue #3055474 by Gribnif, Berdir: template_preprocess_file_link will...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.