Function file_file_download() calls field_get_items() without specifying field's language. This causes the field_get_items() to return items for the current UI's language. But the actual file that's being checked can use different language.

This makes impossible to download files using file_download() for entities that use entity translation and have different language versions of files attached.

The file_file_download() function should call field_get_itmes() with appropriate langcode. The problem is that the langcode of the file is unknown when file_file_download() calls field_get_items(), so instead of using field_get_itmes(), ALL languages should be interrogated to find correct field item.

This is the offending code:

          if ($entity) {
          // Load all field items for that entity.
          $field_items = field_get_items($entity_type, $entity, $field_name);dpm($entity);

          // Find the field item with the matching URI.
          foreach ($field_items as $item) {
            if ($item['uri'] == $uri) {
              $field_item = $item;
              break;
            }
          }
        }

Proposed solution (dirty code):

          if ($entity) {
          // Load all field items for ALL languages for that entity.
          $field_items = array();
          foreach($entity->{$field_name} as $items) {
            $field_items = array_merge($field_items, $items);
          }
          // Find the field item with the matching URI.
          foreach ($field_items as $item) {
            if ($item['uri'] == $uri) {
              $field_item = $item;
              break;
            }
          }
        }
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

SiliconMind’s picture

Status: Active » Needs review
FileSize
783 bytes

Here is a patch for this

Status: Needs review » Needs work

The last submitted patch, patch-for-2112287.diff, failed testing.

SiliconMind’s picture

Status: Needs work » Needs review
FileSize
818 bytes

New patch to address automated testing issue.

matsbla’s picture

I work on a multilingual website where I use the Media Translation module to translate different media files like PDFs. Then the visitor can download the content in different languages, and this patch makes it possible to do it. This works great with this patch, and I think it is good if language codes are declared when downloading files - it will make Drupal more flexible when developing multilingual sites.

I also plan to port this site to Drupal 8 when it is ready, so hope it will not be a problem there too!

SiliconMind’s picture

Version: 7.23 » 7.26
Related issues: +#2215507: Downloads broken for translated private files

I've checked to see if D8 is also affected by this issue. There is the same problem with D8 but it seems that the cause is different so I created a new issue for D8 #2215507: Downloads broken for translated private files.

Gábor Hojtsy’s picture

Posted a cross-note at the issue so people recognize this one.

marksmith’s picture

This problem still occurs with Drupal 7.52.

tatarbj’s picture

Version: 7.26 » 7.x-dev
Assigned: Unassigned » tatarbj
Status: Needs review » Needs work
tatarbj’s picture

Assigned: tatarbj » Unassigned
Status: Needs work » Needs review
FileSize
934 bytes

The original patch, even it was a bit dirty, solved the issue and it works on my local well.
To make it cleaned-up and closer to the D8 version, i've changed the comments and made it simpler.
Please review!

Status: Needs review » Needs work

The last submitted patch, 9: file_file_download_does_not_specify_language-2112287-9.patch, failed testing.

tatarbj’s picture

Oh, i've forgotten to iterate on all the available items of the field, so fixing it now and attaching an interdiff.