title duplicate detection via autocomplete (Drupal7 Version)

Events happening in the community are now at Drupal community events on www.drupal.org.
You are viewing a wiki page. You are welcome to join the group and then edit it. Be bold!

anbei der Beispielcode für node/add autocomplete title field für Nodes vom Typ page.

Man braucht nur eine hook_form_alter, einen hook_menu und eine Funktion, die existierende Nodes diese Types aus der DB ausliest.

function autocomplete_form_alter(&$form, $form_state, $form_id){
function autocomplete_menu() {
function _autocomplete_page_title($string = '') {

function autocomplete_form_alter(&$form, $form_state, $form_id){

switch($form_id){
    case 'page_node_form':
        $form['title']['#autocomplete_path'] = 'autocomplete/page-title';
break;
}   

}

/**
* Menu definition for the autocomplete suggestions
*/
function autocomplete_menu() {

$items['autocomplete/page-title'] = array(
'page callback' => '_autocomplete_page_title',
'access callback' => TRUE,
'type' => MENU_CALLBACK
);

return $items;
}

/**
* Menu callback; Retrieve a JSON object containing autocomplete suggestions for existing pages.
*/
function _autocomplete_page_title($string = ''") {
$matches = array();
if ($string) {

$query = "
    SELECT title, type FROM {node} n WHERE (n.type = 'page') AND LOWER(n.title) LIKE LOWER(:string)
";

    $string .= '%%';
$result = db_query($query, array(':string' => $string));
foreach ($result as $record) {
  $matches[$record->title] = check_plain($record->title);
}

}
drupal_json_output($matches);
}

München

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: