function blockreference_autocomplete_validate in Block reference 7
Same name and namespace in other branches
- 6 blockreference.module \blockreference_autocomplete_validate()
- 7.2 blockreference.module \blockreference_autocomplete_validate()
Validation callback for a blockreference autocomplete element.
1 string reference to 'blockreference_autocomplete_validate'
- blockreference_field_widget_form in ./
blockreference.module - Implements hook_field_widget_form().
File
- ./
blockreference.module, line 425 - Defines a field type for referencing a block from a node.
Code
function blockreference_autocomplete_validate($element, &$form_state, $form) {
$field = field_info_field($element['#field_name']);
$instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
$current_bid = $element['#default_value'];
$value = $element['#value'];
$bid = NULL;
if (!empty($value)) {
// Check whether we have an explicit "[bid:n]" input.
preg_match('/^(?:\\s*|(.*) )?\\[\\s*bid\\s*:\\s*(\\d+)\\s*\\]$/', $value, $matches);
if (!empty($matches)) {
// Explicit bid. Check that the 'title' part matches the actual title for
// the bid.
list(, $title, $bid) = $matches;
if (!empty($title)) {
$result = db_query('SELECT module, delta FROM {block} WHERE bid = :bid', array(
':bid' => $bid,
));
$block = $result
->fetchObject();
$info = module_invoke($block->module, 'block_info');
$real_title = $info[$block->delta]['info'];
if (trim($title) != trim($real_title)) {
form_error($element, t('%name: Title mismatch. Please check your selection.', array(
'%name' => $instance['label'],
)));
}
}
}
else {
// No explicit bid (the submitted value was not populated by autocomplete
// selection). Get the bid of a referencable block from the entered title.
$bids = _blockreference_potential_references($field, array(
$current_bid,
), FALSE, $value, TRUE);
if ($bids) {
// @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...
$bid = $bids ? key($bids) : 0;
}
else {
form_error($element, t('%name: Found no valid block with that title.', array(
'%name' => $instance['label'],
)));
}
}
}
// Set the element's value as the node id that was extracted from the entered
// input.
form_set_value($element, $bid, $form_state);
}