Last active
December 22, 2015 20:08
-
-
Save GoZOo/6523923 to your computer and use it in GitHub Desktop.
Manage media field with form api forms for Drupal 7 administration pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Manage media field with form api forms for administration pages | |
| * Depends of media module http://dgo.to/media | |
| **/ | |
| /** | |
| * Function called by drupal_get_form() | |
| **/ | |
| function myform($form, &$form_state) { | |
| // Get fid from variable | |
| $myform_mediafile_media1 = variable_get('myform_mediafile_media1', array()); | |
| // Build one media field | |
| $form['myform_mediafile_media1'] = array( | |
| '#title' => t("Fichier 1"), | |
| '#description' => t("Fichier 1 description"), | |
| '#type' => 'media', | |
| '#tree' => TRUE, | |
| '#value' => $myform_mediafile_media1, | |
| ); | |
| // Get fid from variable | |
| $myform_mediafile_media2 = variable_get('myform_mediafile_media2', array()); | |
| // Build another media field | |
| $form['myform_mediafile_media2'] = array( | |
| '#title' => t("Fichier 2"), | |
| '#description' => t("Fichier 2 description"), | |
| '#type' => 'media', | |
| '#tree' => TRUE, | |
| '#value' => $myform_mediafile_media2, | |
| ); | |
| // Add submit hook function | |
| $form['#submit'][] = 'myform_submit'; | |
| return system_settings_form($form); | |
| } | |
| function myform_submit($form, &$form_state) { | |
| // Manually put new fid in input values | |
| if (isset($form_state['input']['myform_mediafile_media1']['fid'])) { | |
| $form_state['values']['myform_mediafile_media1']['fid'] = $form_state['input']['myform_mediafile_media1']['fid']; | |
| } | |
| if (isset($form_state['input']['myform_mediafile_media2']['fid'])) { | |
| $form_state['values']['myform_mediafile_media2']['fid'] = $form_state['input']['myform_mediafile_media2']['fid']; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment