Followed this to http://drupal.org/node/1354202 to add files with node.create. It works for me to create, but if I follow the same method for adding new files to my node on update, they will only be created (added to file_managed) but not connected to the node. But if I update title or description on an existing file it will be updated. It seems like you only can specify fid for the files when creating, not updating. It will skip those files.

It seems like the form_builder method strips the field_files from the new files when I do node.update?

Here is the array for the PUT. fid 185 was added one node.create and here I changed the title and the title for that file is updated. But the other file which is new for the update request, it is not added to the node when updated. Any ideas? Do I need to set a flag on the node or the field for it to store "new files"?

[field_files] => Array
(
  [und] => Array
  (
    [0] => Array
      (
        [fid] => 185
        [display] => 1
        [title] => My name
        [description] =>
      )
   
    [1] => Array
      (
        [fid] => 186
        [display] => 1
        [title] =>
        [description] => My description
      )
  )
)
Files: 
CommentFileSizeAuthor
#11 user.update_fid_fix-1433254-11.patch798 byteslex0r
PASSED: [[SimpleTest]]: [MySQL] 2,004 pass(es).
[ View ]

Comments

kylebrowning’s picture

Version:7.x-3.1» 7.x-3.x-dev
Category:support» bug
drewschmaltz’s picture

workaround:

foreach($form_state['values'] as $key => $value) {
  if(array_key_exists($key,$old_node)) {
    $old_node->{$key} = $value;
  }
}
jyloo’s picture

wow, i used a whole night trying to troubleshoot this problem until i found your code here.Work like charm!!
Now my update page can delete and add in new item well.

thanks.

btw if i use this code, would the field still perform the validation part correctly?

farhadhf’s picture

Hi,
I'm having the same problem, where should I put the code in #2? I have this code right now:

<?php
function mymodule_node_submit($node, $form, $form_state) {
  foreach(
$form_state['values'] as $key => $value) {
    if(
array_key_exists($key,$node)) {
     
$info = field_info_field($key);
      if (
$info && ($info['type'] == 'image' || $info['type'] == 'file')) {
       
$new_values = array(LANGUAGE_NONE => array());
       
// $value contains an item with fid=0, we don't wanna insert an empty file into file_managed table. taking it out!
       
foreach ($value[LANGUAGE_NONE] as $item) {
          if (isset(
$item['fid']) && $item['fid'] > 0) {
           
$new_values[LANGUAGE_NONE][] = $item;
          }
        }
       
$node->{$key} = $new_values;
      }
    }
  }
}
?>

but it's not working.

Thanks!

emilkarl’s picture

My guess is you should update the node resource in Services module...

File: services/resources/node_resource.inc
Function: _node_resource_update
Line: No idea?

emilkarl’s picture

Where should this go? Is it fixed in 7.x-3.3?

farhadhf’s picture

Hi,
I changed my code and now it's working, maybe this will help someone later!

<?php
/**
 * Implements hook_node_submit().
 */
function mymodule_node_submit($node, $form, $form_state) {
  foreach(
$form_state['input'] as $key => $value) {
    if(
array_key_exists($key,$node)) {
     
$info = field_info_field($key);
      if (
$info && ($info['type'] == 'image' || $info['type'] == 'file')) {
       
$new_values = array(LANGUAGE_NONE => array());
        foreach (
$value[LANGUAGE_NONE] as $item) {
          if (isset(
$item['fid']) && $item['fid'] > 0 && file_load($item['fid'])) {
           
$new_values[LANGUAGE_NONE][] = $item;
          }
        }
       
$node->{$key} = $new_values;
      }
    }
  }
}
?>
farhadhf’s picture

Issue summary:View changes

More accurate description

wedge’s picture

Issue summary:View changes

For the archives: I put the code from #2 (thanks drewschmaltz) right before the call to drupal_form_submit($node_type . '_node_form', $form_state, $old_node); in _node_resource_update().

And that seems to work.

lex0r’s picture

Priority:Major» Critical

I can't believe this major issue is still active and without a patch :)
I can confirm I see the same situation with user.update, which can promote this issue to "critical". I will post a patch for user.update here later.

lex0r’s picture

Title:Node.update ignores new files» Node.update and user.update ignores new files
lex0r’s picture

StatusFileSize
new798 bytes
PASSED: [[SimpleTest]]: [MySQL] 2,004 pass(es).
[ View ]

Here is a patch for 7.x-3.11 that handles user file updates. Use it very cautiously as it's not a proper way of fixing the issue. It was impossible to bind to hook_user_submit() like above, because this hook just doesn't exist.

kylebrowning’s picture

Status:Active» Needs review