4

I created a small custom module to help users while creating a specific content type. Basically the module just adds the autocomplete feature to the title field to show possible duplicate nodes to the user while filling out the form for "on-the-fly" feedback.

The problem is that the autocomplete only works for users with the permission 'Administer nodes'. Is this normal or am I doing something wrong? If it is normal, is there another way to achieve these results without handing out that permission?

Custom module:

<?php
/**
 * Implementation of hook_menu()
 */
function duplicates_menu() {
  $items = array();
  $items['duplicates/check'] = array(
    'title' => 'Check for duplicates',
    'page callback' => 'duplicates_check',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_form_alter()
 */
function duplicates_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'company_node_form') {
    $form['title']['#autocomplete_path'] = 'duplicates/check';
  }
}

/**
 * Check for duplicates
 */
function duplicates_check($check) {
  $duplicates = array();
  if ($check) {
    $results = db_query('SELECT title FROM {node} WHERE type = "company" AND LOWER(title) LIKE LOWER("%%%s%%")', $check);
    while ($dupes = db_fetch_object($results)) {
      $duplicates[$dupes->title] = check_plain($dupes->title);
    }
  }
  print drupal_json($duplicates);
}

1 Answer 1

2

Avoid a custom implementation and use the Unique Field Module. Once this module is enabled, it adds a 'Unique field settings' section to each content type's administration page for specifying which fields (Including node title) for that content type must be unique.The administrator may choose whether the fields must be unique among all other nodes, among nodes of the same content type, or of the same language. You can also set it to display the nodes that match the title.

2
  • Nice module, however, possible duplicates need to be shown while the user is typing the title and this module doesn't seem to do that
    – Laxman13
    Commented Jul 19, 2011 at 1:36
  • Yeah. It only shows duplicates after they try to submit.
    – user842
    Commented Jul 19, 2011 at 2:25

Your Answer

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

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