I am working on a project which requires adding fields to "product" content type dynamically. That means I can not add fields to this content type from "Manage Fields" but need to use form_alter to add fields to "product_node_form".

One of the field needs to of type media selector. I am using this code:

$form['field_videofile1'] = array(
'#title' => 'File Upload',
'#type' => 'media',

When the form is rendered it is displaying uploading options and selected file is also getting uploaded to specified folder.

Now after uploading is complete, when I am submitting the form, value of this field is not coming in form state or in POST. I have investigated a little and find that there is an invisible "fid" field is generated for each media selector by "media_element_process" function. In my case name of this field should be field_videofile1_fid. But it is only "fid". As a result I am not getting value of this field in form state.

Please suggest a way out.I am really struck at this point.

media.JPG108.73 KBjoyofkolkata
Members fund testing for the Drupal project. Drupal Association Learn more


zonesny’s picture


ParisLiakos’s picture

Version: 7.x-1.2 » 7.x-2.x-dev
 * Implements hook_element_info().
function media_element_info() {
  $types = array();
  $types['media'] = array(
    '#input' => TRUE,
    '#process' => array('media_element_process'),
    //'#value_callback' => 'media_element_value',
    '#element_validate' => array('media_element_validate'),
    '#theme_wrappers' => array('container'),
    '#progress_indicator' => 'throbber',
    '#extended' => FALSE,
    '#required' => FALSE,
    '#media_options' => array(
      'global' => array(
        'types' => array(), // Example: array('image', 'audio');
        'schemes' => array(), // Example: array('http', 'ftp', 'flickr');
    '#attributes' => array(
      'class' => array('media-widget'),
    '#attached' => array(
      'library' => array(
        array('media', 'media_browser'),
  return $types;

If you check value_callback is commented..we need this one for this to work...something like

jm.federico’s picture

Any news on this?

Media module is quite awesome, but this is limiting the module integration with other forms not using entity fields.


ParisLiakos’s picture

Title: Adding #type = 'media' to custom field in D7 » Implement a value_callback for media element
gmclelland’s picture

@ParisLiakos -
Just an fyi... when using the media_multiselect module + plupload + media-2.x-dev and file_entity-2.x-dev, I currently can't seem to specify a "File directory" on image fields. If I do specify a directory, ex. "galleries" then only the first image is uploaded on a multivalued(unlimited) image field and I don't believe the image is uploaded correctly. If you look under admin/content/file the files is missing a name. I believe this problem is only with image fields.

To fix the problem - you can manually create a directory called "galleries", then everything will work correctly UNLESS you are using Tokens in the "File directory" field. Ex. galleries/[current-date:custom:M-Y] is what I use. The problem is that the directories don't get created, so the files can't be moved into the correct directory.

The way I have to work around this is by using the patch for plupload http://drupal.org/node/1946298#comment-7192092, but that is closed(won't fix).

This issue is spread out in different modules and is a little hard to track down.
#1733472: Problem with subdirectory for upload images
#1776300: Respect field settings like minimum - maximum size when used as image field
#1946298: upload_location directory not being created if it doesn't exist
I think I have seen some other similar issues in the plupload and media_multiselect issue queues related to this problem.

pikku-h’s picture


windmaomao’s picture

I actually get it working by the following way.

1) to make sure you can find the form id after submission. You can use "#tree" on this media selector element. This way the submission fid will be wrapped inside an array.

2) to make sure you can grab the fid after the submission, especially if you have multiple media selector on the form, you need to use $form_state['input'][form_element_id_you_use]['fid'] instead of $form_state['values']['fid'] which sometimes doesn't get populated at all when you have multiple selectors.

Yes, media selector has various issues, ex. it doesn't use 'default_value' (instead 'value' is used). But overall it's pretty solid, just needs some improvement.

Devin Carlson’s picture

Component: Media field » Code
Issue summary: View changes
Status: Active » Fixed
Related issues: +#2187837: Bring media element and field widget inline with managed_file equivalents

Status: Fixed » Closed (fixed)

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