function media_field_widget_process_multiple in D7 Media 7.3
Same name and namespace in other branches
- 7.4 includes/media.fields.inc \media_field_widget_process_multiple()
- 7.2 includes/media.fields.inc \media_field_widget_process_multiple()
An element #process callback for a group of media fields.
Adds the weight field to each row so it can be ordered and adds a new Ajax wrapper around the entire group so it can be replaced all at once.
1 string reference to 'media_field_widget_process_multiple'
- media_field_widget_form in includes/
media.fields.inc - Implements hook_field_widget_form().
File
- includes/
media.fields.inc, line 334 - Provide media selector widget and media field formatters to the fields API.
Code
function media_field_widget_process_multiple($element, &$form_state, $form) {
// In order to support multiple selection, we need to reconstruct the _POST
// data that is checked in media_attach_file(). We need to reconstruct the
// field's _POST key name, for example: field_mediafield_und_0.
$upload_name_prefix = implode('_', $element['#parents']) . '_';
$upload_name = $upload_name_prefix . $element['#file_upload_delta'];
if (!empty($_POST['media'][$upload_name])) {
$files = explode(',', $_POST['media'][$upload_name]);
$count = count($files);
// Supposing #file_upload_delta is always the last delta this will work.
for ($i = 0; $i < $count; $i++) {
// For each file selected, increment the field key to be processed.
// field_mediafield_und_0 becomes field_mediafield_und_1, etc.
$_POST['media'][$upload_name_prefix . ($element['#file_upload_delta'] + $i)] = $files[$i];
// Copy the default file element to each newly selected file position.
$default_element = $element[$element['#file_upload_delta']];
$element[] = array_merge($default_element, array(
'#weight' => $element['#file_upload_delta'] + $i + 1,
));
}
}
$element_children = element_children($element, TRUE);
$count = count($element_children);
foreach ($element_children as $delta => $key) {
if ($key != $element['#file_upload_delta']) {
$description = _media_field_get_description_from_element($element[$key]);
$element[$key]['_weight'] = array(
'#type' => 'weight',
'#title' => $description ? t('Weight for @title', array(
'@title' => $description,
)) : t('Weight for new file'),
'#title_display' => 'invisible',
'#delta' => $count,
'#default_value' => $delta,
);
}
else {
// The title needs to be assigned to the attach field so that validation
// errors include the correct widget label.
$element[$key]['#title'] = $element['#title'];
$element[$key]['_weight'] = array(
'#type' => 'hidden',
'#default_value' => $delta,
);
}
}
// Add a new wrapper around all the elements for Ajax replacement.
$element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">';
$element['#suffix'] = '</div>';
return $element;
}