1

I have a custom form for with media selector from media module. The form field is as below.

$form['image'] = array(
      '#title' => t('Image'),
      '#type' => 'media',
      '#description' => t('The uploaded image will be displayed on the page unless it is marked as "removed".'),
      '#default_value' => array('fid' => $current_image),
      '#media_options' => array(
        'global' => array(
          'types' => array(
            'image' => 'image',
          ),
          'enabledPlugins' => array(
            'upload' => 'upload',
            'media_default--media_browser_1' => 'media_default--media_browser_1',
          ),
          'schemes' => array(
            'public' => 'public',
          ),
          'file_directory' => 'subpage_banner',
          'file_extensions' => 'png gif jpg jpeg',
          'max_filesize' => '1 MB',
          'uri_scheme' => 'public',
        ),
      ),
  );

When I select an image and post the form, I get the field value as an empty array. below is the print shot of my $form_state variable.

Array
(
    [identity] => 9
    [path] => contact
    [image] => Array
        (
            [fid] => 0
        )

    [fid] => 0
    [language] => en
    [removed] => 0
    [submit] => Save
    [form_build_id] => form-zFkFCgBCHtesrAYOQrQXC3Ug_sMBg68dwgWwRE-Y2Ls
    [form_token] => i_aIpfgcwSz-7d_H0U48C5SUOkWlVAkBTz1M8yokXtg
    [form_id] => sitegeneral_settings_banner_sub_update_form
    [op] => Save
)

How do I get the selected image's fid?

  • Dear Triskelion, I think there is no point of closing this question as "too localized". This is a common question asked everywhere but with different context. This problem happens because this item is left without documentation afaik. finally I found the answer from drupal.org community. – Muneer Mar 12 '13 at 7:59
1

Added the following code

'#tree' => TRUE,

working charm!!

added after:

'#title' => t('Image'),
'#type' => 'media',
'#description' => t('...'),
  • 1
    Add this code where? Please explain your answer. – niksmac Mar 10 '13 at 11:32

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