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;
}
}
}
Comment | File | Size | Author |
---|---|---|---|
#11 | interdiff.txt | 942 bytes | tatarbj |
#11 | file_file_download_does_not_specify_language-2112287-11.patch | 1.12 KB | tatarbj |
Comments
Comment #1
SiliconMind CreditAttribution: SiliconMind commentedHere is a patch for this
Comment #3
SiliconMind CreditAttribution: SiliconMind commentedNew patch to address automated testing issue.
Comment #4
matsbla CreditAttribution: matsbla commentedI 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!
Comment #5
SiliconMind CreditAttribution: SiliconMind commentedI'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.
Comment #6
Gábor HojtsyPosted a cross-note at the issue so people recognize this one.
Comment #7
marksmith CreditAttribution: marksmith commentedThis problem still occurs with Drupal 7.52.
Comment #8
tatarbjComment #9
tatarbjThe 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!
Comment #11
tatarbjOh, i've forgotten to iterate on all the available items of the field, so fixing it now and attaching an interdiff.