You are here

function bat_calendar_reference_autocomplete_unit_type_validate in Booking and Availability Management Tools for Drupal 7

Validate unit type.

1 string reference to 'bat_calendar_reference_autocomplete_unit_type_validate'
bat_calendar_reference_field_widget_form in modules/bat_calendar_reference/bat_calendar_reference.module
Implements hook_field_widget_form().

File

modules/bat_calendar_reference/bat_calendar_reference.module, line 915
Defines a field type for referencing event information.

Code

function bat_calendar_reference_autocomplete_unit_type_validate($element, &$form_state, $form) {
  $parents = $element['#array_parents'];
  array_pop($parents);
  $field_widget = drupal_array_get_nested_value($form, $parents);
  $field = field_widget_field($field_widget, $form_state);
  $instance = field_widget_instance($field_widget, $form_state);
  $value = $element['#value'];
  $type_id = NULL;
  if (!empty($value)) {

    // Check whether we have an explicit "[type_id:n]" input.
    preg_match('/^(?:\\s*|(.*) )?\\[\\s*type_id\\s*:\\s*(\\d+)\\s*\\]$/', $value, $matches);
    if (!empty($matches)) {
      list(, $title, $type_id) = $matches;
      if (!empty($title)) {
        $unit = bat_type_load($type_id);
        $real_title = $unit->name;
        if (trim($title) != trim($real_title)) {
          form_error($element, t('%name: title mismatch. Please check your selection.', array(
            '%name' => $instance['label'],
          )));
        }
      }
    }
    else {
      $options = array(
        'string' => $value,
        'match' => 'equals',
        'limit' => 1,
      );
      $references = bat_calendar_reference_unit_types_potential_references($field, $options);
      if ($references) {
        $unit_id = key($references);
      }
      else {
        form_error($element, t('%name: unable to find a unit type with that title.', array(
          '%name' => $instance['label'],
        )));
      }
    }
  }
  form_set_value($element, $type_id, $form_state);
}