5

Much in the same way that it is on this site (as you type your question, it shows similar questions) is there a module that will search for similar nodes as I type the node title in Drupal?

I know of the Unique Field module, however it only checks after the node has been submitted. I need some way of doing it as they type.

2 Answers 2

5

There is module Uniqueness , it does exactly what you want. It allows you to enable for any content type, and you can configure checking on node add or edit or both.

Uniqueness module provides a way to avoid duplicate content on your site by informing a user about similar or related content during creation of a new post. A UI widget is added to the node/add form that does asynchronous searches on inputted fields (like the node title or vocabularies) and returns the titles of similar content.

1
  • 2
    Awesome module, great find!
    – Clive
    May 18, 2013 at 8:56
4

I'm not sure if there's a contributed module already. However you can create it yourself.

First you need to create/register autocomplete callback menu using hook_menu function.

/**
 * Implement hook_menu().
 */
function mymodule_menu() {

  $items['mymodule-autocomplete-title'] = array(
    'page callback' => 'mymodule_autocomplete_title',
    'access arguments' => array('view published content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

Then create a function to search and retrieve similar node titles.

function mymodule_autocomplete_title($title) {
  $results = array();
  $query = db_select('node', 'n')
    ->condition('n.title', '%' . db_like($title) . '%', 'LIKE')
    ->fields('n', array('title'));

  $nodes = $query->execute();

  foreach ($nodes as $row) {
    $results[$row->title] = check_plain($row->title);
  }

  drupal_json_output($results);
}

Then alter the desired node add form using hook_form_alter function to change normal node title field to autocomplete node title field.

/**
 * Implement hook_form_alter().
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {

  // node add form id
  if($form_id == 'page_node_form') {
    // make title field autocomplete field  
    $form['title']['#autocomplete_path'] = 'mymodule-autocomplete-title';
  }  
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.