Preprocess overrides $file

aaron - August 21, 2008 - 02:46
Project:FileField
Version:6.x-3.x-dev
Component:Code
Category:bug report
Priority:critical
Assigned:Unassigned
Status:closed
Description

If creating a phptemplate_preprocess_filefield_file, the $file array passed to the function will be overwritten by the time it gets to the tpl.php file with the name of the tpl.php file.

Unfortunately, I think this is a flaw in the design of the preprocess structure, but even if others agree, I'm certain this won't be changed in Drupal 6, as it would entail an API change. However, if anyone wants to interject their own $variables into the preprocess process, they will experience much frustration unless they realize this.

To replicate, you can try the following:

Create a function in template.php with something like the following:

<?php
function phptemplate_preprocess_filefield_file(&$variables) {
 
$variables['original_file'] = $variables['file'];
}
?>

Create a theme file name filefield_file.tpl.php, with the following code (with original copied from theme_filefield_file):

<?php
 
// Note that all of this fails, because $file has been overwritten.
 
$path = $file['filepath'];
 
$url = file_create_url($path);
 
$icon = theme('filefield_icon', $file);
  print
'<div class="filefield-file clear-block">'. $icon . l($file['filename'], $url) .'</div>';
 
 
drupal_set_message('The original $file: '. print_r($original_file, TRUE));
 
drupal_set_message('The overwritten $file: '. print_r($file, TRUE));
?>

I'm not sure of the best solution to this. The best solution would be to file a request to the Drupal project to perhaps use magic variables, such as $variables['file__'] rather than overwriting potentially damaging variables. However, I strongly doubt at this point that would make it into d6.

The safest route would probably be to rename $file in the function to something else. However, that's inconsistent with the rest of the module.

Another idea would be to simply document a workaround for people wishing to implement a preprocess for functions in this module.

The sticking point is that the current preprocess override of $variables['file'] means we can't use $file even in future module-defined template files.

#1

aaron - August 21, 2008 - 02:54

I opened an issue for this in the Drupal project, just in case: #297952: Preprocess function overrides $variables['file'].

#2

drewish - October 10, 2008 - 16:19
Version:6.x-3.0-alpha3» 6.x-3.x-dev

bumping this since it's a blocker

#3

aaron - October 10, 2008 - 16:34

actually, i think it's been committed to d6. should be in d6.5. that issue is now to port it to d7.

#4

dopry - October 31, 2008 - 04:44
Status:active» closed

The we'll call this closed.. thanks for the heads up aaron.

 
 

Drupal is a registered trademark of Dries Buytaert.