0

I have a form with images. It also contains title and alt for 3 languages, so when I select them in one query, I get the same managed file fid 3 times. But that's not the problem.

Relevant part of code is:

kpr($image_has_language);
$first_language = reset($image_has_language);
$form['image'][$i] = array (
   '#type' => 'fieldset',
   '#collapsible' => FALSE,
   'ImageFile' => array(
     '#type' => 'managed_file',
     '#default_value' => (integer)$first_language['ImageFile'],
     '#title' => t("Image"),
     '#title_display' => 'invisible',
     // '#description' => t('Allowed extensions: gif png jpg jpeg.'),
     '#upload_validators' => array(
       'file_validate_extensions' => array('gif png jpg jpeg'),
       'file_validate_size' => array(variable_get('something_image_size', 5 * 1024 * 1024)),
     ),
     '#upload_location' => 'public://something/image',
     '#theme' => 'preview_upload',
   ),
   'id_image' => array(
     '#type' => 'value',
     '#value' => $clone?'new':$first_language['idImage'],
   )
 );
 kpr($form['image'][$i]);

Below results of that kprs:

Screenshot - all good

As you can see, all is perfectly well. File is on disk, file preview work, in database it is marked as permanent and as used.

When I call var_dump on $form_state['values']['image'][0] in validation function, without changing file on the form, I get:

  array(3) {
    ["ImageFile"]=>
    int(0)
    ["id_image"]=>
    string(3) "599"
  }

Values in $_POST are OK:

  ["image"]=>
  array(4) {
    [0]=>
    array(2) {
      ["ImageFile"]=>
      array(1) {
        ["fid"]=>
        string(4) "3696"
      }

If I change file, it works as expected:

  array(3) {
    ["ImageFile"]=>
    string(4) "3698"
    ["id_image"]=>
    string(3) "599"
  }

and in $_POST:

  ["image"]=>
  array(4) {
    [0]=>
    array(2) {
      ["ImageFile"]=>
      array(1) {
        ["fid"]=>
        string(4) "3698"
      }

How can I identify where my value gets removed? I also have another image on that form, single one (so without foreach), and it works OK.

2 Answers 2

1

First of all can you try to remove the fieldset to a different $form[] array key? Is it really necessary to make it nested like this?

I think if you want to know if the file is added or removed then you can check in the following way.

$total_image_count = $form_state['storage']['image_elements']; // The number of image uploaded elements; just as example.
for($i = 0; $i < $total_image_count; $i++) {
  if ($form_state['values']['image'][$i] != 0) {
    // File is not deleted.
  } elseif ($form_state['values']['image'][$i] == 0) {
    //File is deleted.
  }
}

However, check with this and let me know.

6
  • Fieldset is needed, because I need to know which alt and title is for which image. And the very problem is that I'm getting $form_state['values']['image'][$i] == 0 if file is not deleted, but left default. But only on this particular field, so I know there is something wrong, and I just don't know how to debug it, how to find out why between $_POST and $form_state file ID gets forgotten.
    – Mołot
    Jul 22, 2014 at 13:36
  • I still think this is due to the nesting of the fieldset. Can you debug once removing it? Then can be modified to retain the required needs. Also, I can see your comment in drupal.org/node/1957602 which is associated with fieldset nesting. Jul 22, 2014 at 13:41
  • My comment was about a bit different situation (file not uploaded at all). But good catch. I totally forgot about it. Will try as soon as possible, thanks.
    – Mołot
    Jul 22, 2014 at 13:49
  • welcome, if you are able to fix it then please share it so that it may be useful in future. :) Jul 23, 2014 at 5:13
  • I hacked around it so far. If I'll manage to debug it, truly, I will post the process and result as an answer. For now? There is nothing I can be proud about.
    – Mołot
    Jul 23, 2014 at 6:30
1

There is no need to debug anything. It seems it was just an effect of Regression: Files or images attached to certain core and non-core entities are lost when the entity is edited and saved

Solution is to downgrade, or wait.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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