0

I've added a multiple image field to the user account that uses the multiupload_imagefield_widget and it works when I edit the account.

On the other hand, I have a custom module with a form that I would like to edit just this field (I have a submit handler to update the user data). I call user_load to display the data and I can see the data is there in the debugger, but no images are displayed on the form.

What do I have to do to make them show up?

Here's my code:

$user_data = user_load($user->uid);
$form['my_imageGallery'] = array(
  '#type'             => 'image',
  '#title'            => t('Image Gallery'),
  '#default_value'    => isset($user_data->field_galeria) ? $user_data->field_galeria : null,
  '#upload_location'  => 'public://user_galleries/',
  '#upload_validators' => array('file_validate_extensions' => array('png jpg gif')),
);
2

1 Answer 1

0

I think you need to use a managed_file form element.

$user_data = user_load($user->uid);
$form['my_imageGallery'] = array(
  '#type'             => 'managed_file',
  '#title'            => t('Image Gallery'),
  '#default_value'    => isset($user_data->field_galeria) ? $user_data->field_galeria : null,
  '#upload_location'  => 'public://user_galleries/',
  '#upload_validators' => array('file_validate_extensions' => array('png jpg gif')),
);
6
  • Thanks, but that wasn't it.
    – pglatz
    Commented Jul 21, 2020 at 23:01
  • I'm seeing the error: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() when I load the form to edit.
    – pglatz
    Commented Jul 21, 2020 at 23:22
  • @pglatz I was looking into it and you need to edit the value for #default_value. You can't just set it to a file entity. Rather, you needs to be formatted as #default_value => [ 'fids' => [ 12, 23, 34, ... ] ]. See this question Commented Jul 22, 2020 at 0:16
  • I tried it and wouldn't get it to work; my form's field looks like this (partial): [#type] => managed_file [#title] => Image Gallery [#default_value] => Array ( [fids] => Array ( [0] => 1195 [1] => 1196 [2] => 1197 ) ) ... So I saw this cleancode.co.nz/blog/1096/… and tried creating three fields for there with a default value that is a scalar.
    – pglatz
    Commented Jul 22, 2020 at 19:38
  • The problem with your example was when I used an array for the fids I got the array_flip error. Thanks for your suggestion. I'd prefer a single field with the array, but it may not be possible.
    – pglatz
    Commented Jul 22, 2020 at 19:40

Your Answer

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

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