Scenario # 1 - Unique Node
hook_node_validate () is what you need if you are working with Drupal 7
Either you can simply use this code below in your custom module, or you can extract the repository from unique_title git that you need to pull into your module directory for your project, and then activate the module.
function unique_title_node_validate($node, $form, &$form_state) { if (!isset($node->nid)) { $title = $form_state['values']['title']; $results = db_select('node')->fields('node', array('title'))->condition('title', $title, '=')->execute(); $matches = array(); foreach ($results as $result) { $matches[$result->title] = check_plain($result->title); } if (isset($matches) && !empty($matches)) { form_set_error('title', t('Title must be unique')); } } }
Scenario # 2 - Autocomplete Node Header
hook_form_alter () and hook_menu () can help you with this to autocomplete Node headers while working with Drupal 7.
Either you can simply use this code below in your user module, or you can extract the repository from autocomplete git that you need to pull into your module directory for your project and then activate the module.
In your user module, use the code below:
function module_form_alter(&$form, &$form_state, $form_id) { $form['title']['#autocomplete_path'] = 'unique_node_autocomplete_callback'; } function module_menu() { $items['unique_node_autocomplete_callback'] = array( 'page callback' => 'autocomplete_unique_node_autocomplete_callback', 'file' => 'module.inc', 'type' => MENU_CALLBACK, 'access arguments' => array('access content'), ); return $items; }
In your module.inc file, use the AJAX callback below:
function module_unique_node_autocomplete_callback($string = "") { $matches = array(); if ($string) { $result = db_select('node') ->fields('node', array('nid', 'title')) ->condition('title', db_like($string) . '%', 'LIKE') ->range(0, 10) ->execute(); foreach ($result as $node) { $matches[$node->title . " [$node->nid]"] = check_plain($node->title); } } drupal_json_output($matches); }