1 node.pages.inc | node_autocomplete_validate($string) |
Node title validation handler.
Validate handler to convert our string like "Some node title [3325]" into a nid.
In case the user did not actually use the autocomplete or have a valid string there, we'll try to look up a result anyway giving it our best guess.
Since the user chose a unique node, we must now use the same one in our submit handler, which means we need to look in the string for the nid.
Parameters
string $string: The string to validate.
Return value
$nid: A node ID if matched, or NULL if no match.
See also
File
- core/
modules/ node/ node.pages.inc, line 1094 - Callbacks for adding, editing, and deleting content and managing revisions.
Code
function node_autocomplete_validate($string) {
$matches = array();
$nid = 0;
// This preg_match() looks for the last pattern like [33334] and if found
// extracts the numeric portion.
$result = preg_match('/\[([0-9]+)\]$/', $string, $matches);
if ($result > 0) {
// If $result is nonzero, we found a match and can use it as the index into
// $matches.
$nid = $matches[$result];
// Verify that it's a valid nid.
$node = node_load($nid);
if (empty($node)) {
return NULL;
}
}
// If the input was numeric, check that it matches a node.
elseif (is_numeric($string) && node_load($string)) {
$nid = (int) $string;
}
// Check that the user may have directly entered a node title.
else {
$nid = db_select('node')
->fields('node', array('nid'))
->condition('title', db_like($string) . '%', 'LIKE')
->addTag('node_access')
->range(0, 1)
->execute()
->fetchField();
}
return (!empty($nid)) ? $nid : NULL;
}