You are here

function media_field_widget_submit in D7 Media 7.3

Same name and namespace in other branches
  1. 7.4 includes/media.fields.inc \media_field_widget_submit()
  2. 7.2 includes/media.fields.inc \media_field_widget_submit()

Form submit handler for attach/remove button of media_field_widget_form().

This runs in addition to and after media_field_widget_submit().

See also

media_field_widget_submit()

media_field_widget_form()

media_field_widget_process()

1 string reference to 'media_field_widget_submit'
media_field_widget_process in includes/media.fields.inc
An element #process callback for the media field type.

File

includes/media.fields.inc, line 423
Provide media selector widget and media field formatters to the fields API.

Code

function media_field_widget_submit($form, &$form_state) {

  // During the form rebuild, media_field_widget_form() will create field item
  // widget elements using re-indexed deltas, so clear out $form_state['input']
  // to avoid a mismatch between old and new deltas. The rebuilt elements will
  // have #default_value set appropriately for the current state of the field,
  // so nothing is lost in doing this.
  $parents = array_slice($form_state['triggering_element']['#parents'], 0, -2);
  drupal_array_set_nested_value($form_state['input'], $parents, NULL);
  $button = $form_state['triggering_element'];

  // Go one level up in the form, to the widgets container.
  $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -1));
  $field_name = $element['#field_name'];
  $langcode = $element['#language'];
  $parents = $element['#field_parents'];
  $submitted_values = drupal_array_get_nested_value($form_state['values'], array_slice($button['#parents'], 0, -2));
  foreach ($submitted_values as $delta => $submitted_value) {
    if (!$submitted_value['fid']) {
      unset($submitted_values[$delta]);
    }
  }
  ksort($submitted_values);

  // Re-index deltas after removing empty items.
  $submitted_values = array_values($submitted_values);

  // Update form_state values.
  drupal_array_set_nested_value($form_state['values'], array_slice($button['#parents'], 0, -2), $submitted_values);

  // Update items.
  $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);
  $field_state['items'] = $submitted_values;
  field_form_set_state($parents, $field_name, $langcode, $form_state, $field_state);
}