function media_browser_plus_submit in Media Browser Plus 7
Same name and namespace in other branches
- 7.2 media_browser_plus.module \media_browser_plus_submit()
Submit handler for the media browser forms that create new media entities.
Enhances the media creation process by populating field content for the newly created entities from user-submitted data and/or data available from a remote provider.
See also
media_browser_plus_form_media_add_upload_multiple_alter()
media_browser_plus_form_media_internet_add_alter()
2 string references to 'media_browser_plus_submit'
File
- ./
media_browser_plus.module, line 567 - Adds fields to the media browser forms for better UX
Code
function media_browser_plus_submit($form, &$form_state) {
// Grab the fids of the newly created media entities from the redirect query
// string that was created by the form's primary submit handler, and load the
// corresponding entities.
$fids = $form_state['redirect'][1]['query']['fid'];
if (!is_array($fids)) {
$fids = array(
$fids,
);
}
$media_entities = file_load_multiple($fids);
// If tags have been entered, apply them to each new entity.
if (!empty($form_state['values']['field_tags'])) {
// Create any new taxonomy terms.
foreach ($form_state['values']['field_tags'] as $i => &$item) {
if ($item['tid'] == 'autocreate') {
$term = (object) $item;
unset($term->tid);
taxonomy_term_save($term);
$item['tid'] = $term->tid;
}
unset($item);
}
foreach ($media_entities as $media) {
$media->field_tags[LANGUAGE_NONE] = $form_state['values']['field_tags'];
}
}
// Apply folder.
foreach ($media_entities as $media) {
$media->field_folder[LANGUAGE_NONE] = array(
array(
'tid' => $form_state['values']['field_folder'],
),
);
}
// If the new media is from a 3rd party provider, and that provider also
// provides MRSS data about the media, then populate the title and description
// fields from that data.
if (!empty($form_state['values']['embed_code'])) {
$provider = media_internet_get_provider($form_state['values']['embed_code']);
if ($data = _media_browser_plus_metadata($provider)) {
foreach ($data as $field_name => $value) {
$field = field_info_field($field_name);
// Limiting value population only if the field is of type 'text' or
// 'text_long' isn't as extensible as would be ideal, but we need some
// protection against populating a field with incompatible content.
if (isset($field) && in_array($field['type'], array(
'text',
'text_long',
)) && isset($field['bundles']['media'])) {
foreach ($media_entities as $media) {
if (in_array($media->type, $field['bundles']['media']) && !isset($media->{$field_name}[LANGUAGE_NONE][0]['value'])) {
$media->{$field_name}[LANGUAGE_NONE][0]['value'] = $value;
}
}
}
}
}
}
foreach ($media_entities as $media) {
media_browser_plus_move_file($form_state['values']['field_folder'], $media);
}
}