Invalid argument on CCK fields unrelated to Node type
| Project: | FileField Paths |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | duplicate |
Having installed the latest dev version to test that the recent commits (including my own #564680: Add support for bulk rebuilds of derivative files in Image module) were working, I am getting the following error repeated over 20 times when creating an image node:
warning: Invalid argument supplied for foreach() in C:\Documents and Settings\admin\My Documents\My Websites\btomcc\sites\all\modules\filefield_paths\filefield_paths.module on line 551
I have added two CCK fields to Image on the system in question but I think the errors are generated for fields that aren't associated with my Image content type.
Specifically I think the problem is in this code:
// Get CCK fields.
if (module_exists('content')) {
$cck = content_fields(NULL, $node->type);
foreach ($cck as $name => $field) {
if ($field['type'] == 'text') {
foreach ($node->{$name} as $key => $value) {
if (isset($node->{$name}[$key]['value'])) {
$fields["{$name}_{$key}"] = &$node->{$name}[$key]['value'];
}
}
}
}
}As I understand the comments in content.module, content_fields(NULL, $node->type) does NOT return all fields for just $node->type because the first parameter is NULL. It returns all CCK fields in my system (and there are quite a few).
Then when the line foreach ($node->{$name} as $key => $value) { is executed, $name can be a field name that is not instantiated on $node at all.
I will see whether it is enough just to test whether $node->{$name} is set before attempting to apply foreach to it.

#1
Here is a patch to make the suggested change. Will do a bit more testing as well.
#2
This works with a CCK field on Page and a different one on Image. If the full path of the uploaded file on a test Page node is entered into the CCK field, that field is updated when the node is edited and saved following a change to the path token definition (using the new Update feature).
#3
OK, going to mark this issue as a duplicate because there is a neater solution proposed at http://drupal.org/node/606500#comment-2210098.