For the managed_file example code, I think a call to file_usage_add should be added right after the file_save line. I think it is important to point this out because if that does not occur, when you go back to your form and try to save it (based on the example code given) you will get an error from file_managed_file_validate saying that the file used may not be referenced.

Comments

jhodgdon’s picture

Sounds like a reasonable suggestion. Thanks.

jhodgdon’s picture

Title: Documentation problem with forms_api_reference.html » forms_api_reference.html - managed_file example code needs modification
cesarpo’s picture

Priority: Normal » Major

This hasn't been added to the FAPI documentation, and made me spend the last hour :)

Updated priority so it gets noticed and we save someone from having the same error.

jhodgdon’s picture

Priority: Major » Normal
Issue tags: +FAPI reference

There are a lot of problems with that page, because it's a beast and very difficult to edit. We're trying to resolve that problem, but it hasn't been done yet.

This particular issue may have caused you some pain (and I'm sorry it did), but it is not any more major than any of the other documentation issues.

jhodgdon’s picture

Project: Drupal core » Documentation
Version: 7.x-dev »
Component: documentation » API documentation files

FAPI reference file is in the Documentation project now.

jn2’s picture

Status: Active » Needs review

Here's what's in the managed_file example right now:

  // Load the file via file.fid.
  $file = file_load($form_state['values']['my_file_field']);
  // Change status to permanent.
  $file->status = FILE_STATUS_PERMANENT;
  // Save.
  file_save($file);

Here's how I propose to change it, adding the call to file_usage_add:

  // Load the file via file.fid.
  $file = file_load($form_state['values']['my_file_field']);
  // Change status to permanent.
  $file->status = FILE_STATUS_PERMANENT;
  // Save.
  file_save($file);
  //Record that the module (in this example, user module) is using the file. 
  file_usage_add($file, 'user', 'user', $account->uid);  

(Without the call to file_usage_add, file_managed_file_validate produces an error upon saving the form, saying that the uploaded file may not be referenced.)

Anonymous’s picture

+

sven.lauer’s picture

Status: Needs review » Needs work

Looks good to me, except for the missing space in

 //Record that the module (in this example, user module) is using the file. 

I suppose we need a patch? Or do you want to commit this directly once the change is RTBCed?

sven.lauer’s picture

Status: Needs work » Reviewed & tested by the community

As I just learned in #8 of #800162: Documentation problem with forms_api_reference.html: give function params for all callbacks, forms_api_reference.html is not patched currently. So RTBCing this.

jn2’s picture

Status: Reviewed & tested by the community » Fixed

@sven.lauer
Thanks for the review!

Fixed in both D7 and D8.

sajjo’s picture

but now when i press remove button file is not physically removed from directory and from DB as well. yes field is empty but why not from other sources.

jn2’s picture

@sani723

This issue is just about documentation. If you are having problems with the functionality, you need to find an issue that deals with that or open another issue.

Anonymous’s picture

@sani723 "it's not a bug it's a feature" :)
If it worked like you want then if I'll have some gallery node and I've removed all images attach to it(imagefield) and I didn't hit the submit button I would lost all images instantly. So it's good that it works like it does because you have to confirm(submit) the form so actions(delete) take place. If you want instant remove you can do it in many ways(add #process function for example). But how it works now is better then the other way.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

MohammadMoussa-Lebanon’s picture

Please could you tell me where i have to place your addition , in which file pleas e, this problem is going to make me crazy!!!

jhodgdon’s picture

In the Documentation git repository, the file is under developer/topics.

jhodgdon’s picture

Oh sorry. I thought you were talking about resolving this issue, and I didn't realize you were looking for programming help. This isn't a good place to ask for programming help...

There are several support options listed if you click on "Support" at the top of Drupal.org, which will take you to:
http://drupal.org/support

There you can find out about the Drupal IRC channels, and the Forums, which are our two main support mechanisms in the Drupal community.

Good luck with your issue!

MohammadMoussa-Lebanon’s picture

thanks dear , you teached me :)