You are here

function viewreference_autocomplete_validate in View reference 7.3

Same name and namespace in other branches
  1. 6.3 viewreference.module \viewreference_autocomplete_validate()
  2. 6 viewreference.module \viewreference_autocomplete_validate()
  3. 6.2 viewreference.module \viewreference_autocomplete_validate()

Validation callback for a viewreference_autocomplete element.

1 string reference to 'viewreference_autocomplete_validate'
viewreference_autocomplete_process in ./viewreference.module
Process callback for a viewreference_autocomplete element.

File

./viewreference.module, line 343
Defines a field type for referencing a view from a node.

Code

function viewreference_autocomplete_validate($element, &$form_state, $form) {
  $value = $element['#value'];
  $new_value = NULL;
  if (!empty($value)) {
    $field = field_widget_field($element, $form_state);
    $regex = '/' . '(.*).*?' . '(\\[)' . '((?:[a-z][a-z0-9_]*))' . '(:)' . '((?:[a-z][a-z0-9_]*))' . '(\\])' . '/is';

    // End of string
    preg_match($regex, $value, $matches);
    if (!empty($matches)) {
      $new_value = $matches[3] . ':' . $matches[5];
      $allowed = viewreference_get_views(FALSE, $field['settings']);
      if (!isset($allowed[$new_value])) {
        form_error($element, t('%name: View display %value cannot be referenced.', array(
          '%name' => $element['#title'],
          '%value' => $new_value,
        )));
      }
    }
    else {
      form_error($element, t('%name: The value %value is in an unexpected format.  Expecting: %format', array(
        '%name' => $element['#title'],
        '%value' => $value,
        '%format' => '<em>View title [view:display_n]</em>',
      )));
    }
  }
  form_set_value($element, $new_value, $form_state);
}