I thought it would be handy if there was an auto completion for the 'from' field. The auto completion would get it's data from a list of 404s in the watchdog table. This data is checked to make sure that the 404s are still not a valid menu path or a current redirect. This added functionality also has a test case written for it just to be sure. Here's the PHP code of the auto complete function that I added:
/**
* Autocompletion callback for the add/edit redirect form. Returns a list of
* current 404s on the site.
*/
function path_redirect_autocomplete($string = '') {
$matches = array();
// Get a list of 404s, sorted by the number of times each 404 was processed.
$query = db_query("SELECT message, COUNT(message) AS count FROM {watchdog} WHERE type = 'page not found' AND LOWER(message) LIKE '%%%s%%' GROUP BY message ORDER BY count DESC", drupal_strtolower($string));
while ($path = db_result($query)) {
// If the 404 is now a valid path or already has a redirect, discard it.
if (!menu_get_item($path) && !path_redirect_load(array('path' => $path))) {
$matches[$path] = check_plain($path);
}
}
// Limit the output to 10 results and return the JSON.
$matches = array_slice($matches, 0, 10);
drupal_json($matches);
}
Comments