Using a Media 2.x/4.x widget/browser in a custom Form API form (with options)
Last updated on
3 March 2019
Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites
Here's an example of adding a media browser/widget field to a custom form using the form API, along with passing in options for file extensions and maximum filesize.
$form['image'] = array(
'#type' => 'media',
'#title' => 'Image',
'#description' => 'Description',
'#theme' => 'media_widget', // Without that, you only get text input
'#default_value' => variable_get('image_saved', NULL), //The fid
'#media_options' => array(
'global' => array(
'file_directory' => 'image_folder',
'enabledPlugins' => array( // Not needed if you want to enable all the plugins
'upload',
'media_default--media_browser_my_files',
// Following is the plugin declaration for the Library; commented out to illustrate
// approach of not enabling all plugins.
// 'media_default--media_browser_1'
),
'file_extensions' => 'png gif jpg jpeg',
'max_filesize' => '2 MB',
'uri_scheme' => 'public',
'types' => array('image'),
'schemes' => array(
'public' => 'public',
),
),
),
);
Help improve this page
Page status: No known problems
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion