Filefield seems to handle empty field values in inconsistent ways - sometimes returning an empty array...
$node->field_xyz[0] = NULL

... sometimes returning an array populated with empty values ...
$node->field_xyz[0] = array(fid => NULL, list => NULL, data => NULL,)

... and sometimes returning an array populated with default values ...
$node->field_xyz[0] = array(fid => 0, list => 1, data => 1,)

This is specifically causing problems with the Driven module: see #865388: Filefield reporting all files removed and added . The "fix" that I applied in that issue, as mentioned in comment #37, is to change line ~136 of filefield_field.inc to $items[$delta] = array('fid' => NULL, 'list' => NULL, 'data' => NULL,);. It's seems like there might be a better permanent solution to this problem though... what do you think?

Members fund testing for the Drupal project. Drupal Association Learn more

Comments

arhak’s picture

subscribing

Dane Powell’s picture

Just FYI, it seems that a more appropriate "fix" for the issue I was having is to unset $items[$delta] at that line. At any rate...

obrienmd’s picture

subscribe

quicksketch’s picture

Category: bug » task
Status: Active » Postponed (maintainer needs more info)

Sure I'd agree that handling NULL values would be a good thing. If you provide a patch I'd be happy to give it a look. Other modules can avoid this being a problem by using empty($file['fid']) as their check, since that will always work no matter if $file is NULL or if $file['fid'] is 0 or NULL.

Dane Powell’s picture

Status: Postponed (maintainer needs more info) » Needs review
FileSize
482 bytes

Well there seems to be a number of possible solutions, but this one makes the most sense to me- simply calling content_set_empty on the $items array before it is returned from filefield_field_load

arhak’s picture

@#4

can avoid this being a problem by using empty($file['fid']) as their check

annoying when using foreach, the key exists and still pops up, also other functions fail (if not considered the nullified key) e.g: array_key_exists, array_diff_key, array_intersect_key, and so on
which would require an extra array_filter

@#5

Well there seems to be a number of possible solutions [...]

if CCK takes care of this on presave and view (the later only when previewing)

      // Filter out empty values.
      $items = content_set_empty($field, $items);

perhaps CCK should consider doing it on load as well

PS: how is it handled for D7?

arhak’s picture

@#5 patch

-
+  
+  $items = content_set_empty($field, $items);

trailing spaces on empty line

Dane Powell’s picture

FileSize
425 bytes

Sorry, no trailing space this time...

I agree that the problem seems to be that content_set_empty is never called on the load op, so we should do it manually. Maybe it would be better if CCK did this in core *shrug*. I'm just not familiar enough with CCK to say.

Dane Powell’s picture

Version: 6.x-3.9 » 6.x-3.10
Category: task » bug

The patch in #8 should still apply to 6.x-3.10