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:

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):

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

Comments

aaron’s picture

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

drewish’s picture

Version: 6.x-3.0-alpha3 » 6.x-3.x-dev

bumping this since it's a blocker

aaron’s picture

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

dopry’s picture

Status: Active » Closed (fixed)

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