You are here

function blockreference_autocomplete_validate in Block reference 6

Same name and namespace in other branches
  1. 7.2 blockreference.module \blockreference_autocomplete_validate()
  2. 7 blockreference.module \blockreference_autocomplete_validate()

Validate an autocomplete element.

Remove the wrapper layer and set the right element's value.

1 string reference to 'blockreference_autocomplete_validate'
blockreference_autocomplete_process in ./blockreference.module
Process an individual element.

File

./blockreference.module, line 615
Defines a field type for referencing a block from a node.

Code

function blockreference_autocomplete_validate($element, &$form_state) {
  $field_name = $element['#field_name'];
  $field = content_fields($field_name);
  $field_key = $element['#columns'][0];
  $delta = $element['#delta'];
  $value = $element['#value'][$field_key];
  $bid = NULL;
  if (!empty($value)) {
    preg_match('/^(?:\\s*|(.*) )?\\[\\s*bid\\s*:\\s*(\\d+)\\s*\\]$/', $value, $matches);
    if (!empty($matches)) {

      // explicit bid
      list(, $info, $bid) = $matches;
      $args[] = $bid;
      $result = db_query(db_rewrite_sql("SELECT b.module, b.delta FROM {blocks} b WHERE b.bid = '%s'", 'blocks', 'bid', $args), $args);
      $block = db_fetch_object($result);
      $info = module_invoke($block->module, 'block', 'list');
      $block->info = $info[$block->delta]['info'];
      if (!empty($title) && ($b = db_fetch_object($result)) && $info != $b->info) {
        form_set_error($element[$field_key], t('%name: Title mismatch. Please check your selection.'), array(
          '%name' => t($element[$field_key]['#title']),
        ));
      }
    }
    else {

      // no explicit bid
      // TODO :
      // the best thing would be to present the user with an additional form,
      // allowing the user to choose between valid candidates with the same title
      // ATM, we pick the first matching candidate...
      $bids = _blockreference_potential_references($field, FALSE, $value, TRUE);
      $bid = !empty($bids) ? array_shift(array_keys($bids)) : 0;
    }
  }
  form_set_value($element, $bid, $form_state);
  return $element;
}