You are here

function entityreference_view_widget_validate in Entity Reference View Widget 7

Same name and namespace in other branches
  1. 7.2 entityreference_view_widget.module \entityreference_view_widget_validate()

Validation callback for the widget form. Adjusts the internal ids taking into account the add / remove checkboxes.

1 string reference to 'entityreference_view_widget_validate'
entityreference_view_widget_field_widget_form in ./entityreference_view_widget.module
Implements hook_field_widget_form().

File

./entityreference_view_widget.module, line 463

Code

function entityreference_view_widget_validate($element, &$form_state) {
  $field_name = $element['#field_name'];
  $language = $element['#language'];
  $target_type = $form_state['field'][$field_name][$language]['field']['settings']['target_type'];
  $element_values = $form_state['values'][$field_name][$language];
  $value = array();

  // First add the existing elements, if they are not marked for deletion.
  if (!empty($element_values['items'])) {

    // Sort selected elements by their weight.
    uasort($element_values['items'], '_field_sort_items_helper');
    foreach ($element_values['items'] as $entity_id => $data) {
      if (!$data['remove']) {
        $value[] = array(
          'target_type' => $target_type,
          'target_id' => $entity_id,
        );
      }
    }
  }

  // Then new items (marked for addition).
  if (!empty($element_values['add'])) {
    $add = array_filter($element_values['add']);
    foreach ($add as $entity_id => $status) {
      if (!isset($element_values['items'][$entity_id])) {
        $value[] = array(
          'target_type' => $target_type,
          'target_id' => $entity_id,
        );
      }
    }
  }
  form_set_value($element, $value, $form_state);

  // Update items.
  $parents = array_slice($element['#parents'], 0, -2);
  $field_state = field_form_get_state($parents, $field_name, $language, $form_state);
  $field_state['items'] = $value;
  field_form_set_state($parents, $field_name, $language, $form_state, $field_state);
}