1

I have a custom content type, and the relative form for creating/editing contents. It contains a file field. I need to check, during hook

 function mymodule_myform_form_validate($form, $form_state){...}

if the user changed the file with another (while editing the content). This can be done by checking if the fid has changed, and the fid of the new file can be found, for example, in

 $lang_field = $form['my_field']['#language'];
 $file_fid = $form_state['input']['my_field'][$lang_field][0];

but what about the fid of the old file? Exploring form and form_state array i found several occurrences of the old and the new fid, but i am not sure which i'm supposed to use. I am not even certain that the code above is the correct one for retrieving the fid of the new file... There is a standard (safe) way to retrieve these informations?

1

There are 2 approach we can look into:

  1. Query the DB using db_query in field table, like if your field machine name is field_image, then its field will be field_data_field_image, and get fid of a particular node, in this case fid will be in field_image_fid

    SELECT field_image_fid from field_data_field_image WHERE entity_id=NODE_ID

It will get u previous fid, and u can validate

  1. Load Node from nid, either using node_load or entity_load_single, from here you will get your Old FID, as image has not yet been attached to the node, it has only been saved in file_managed Table.
  • not a bad idea, but i saw that the informations i need are already in either of the two arrays, so it would be less resource-wasting taking them directly... isn't? – Sasha Grievus Jan 7 '16 at 23:10
  • 1
    Yes, if it is previously there, then loading node or fetching data will be resource wasting.. – Manoj Bisht Jan 8 '16 at 3:59
1

Finally i found an explanation of the form_state array here

form_state

Unfortunately is not so complete... better than nothing.

To get the fid of the new file i did

$lang_field_package = $form['my_field']['#language'];
$fid_new = $form_state['values']['my_field'][$lang_field_package][0]['fid'];

values: An associative array of values submitted to the form. The validation functions and submit functions use this array for nearly all their decision making. (Note that #tree determines whether the values are a flat array or an array whose structure parallels the $form array. See Form API reference for more information.) These are raw and unvalidated, so should not be used without a thorough understanding of security implications. In almost all cases, code should use the data in the 'values' array exclusively. The most common use of this key is for multi-step forms that need to clear some of the user input when setting 'rebuild'. The values correspond to $_POST or $_GET, depending on the 'method' chosen.

This has proven safer than 'input'. Using 'input', when the user chose a file without pressing the 'upload' button beside the file field, the fid was retrieved as zero instead of the chosen file fid, while in 'values' the fid was correct. It does make sense, but, whether you press upload or not, the file would be load on submit, so i required the right fid anyway.

To get the fid of the old file i did

$lang_field_package = $form['my_field']['#language'];
if (!empty($form_state['build_info']['args'][0]->my_field)) {
  $fid_old = $form_state['build_info']['args'][0]->my_field[$lang_field_package][0]['fid'];
}

build_info: Internal. An associative array of information stored by Form API that is necessary to build and rebuild the form from cache when the original context may no longer be available: args: A list of arguments to pass to the form constructor.

  • For me: $form_state->getValue('my_field')[0]['fids'] did work. That gives you an array with the uploaded file ID(s). – plocks Feb 11 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.