0

how to prevent user creating a duplicate node title for a content type?

I already try uniqueness module (https://drupal.org/project/uniqueness) but it's only list out the duplicate content title but not return any error message when user save the new content. Any suggestion module or code? :)

1

You can try the Unique field module.

This module performs additional validation when a node is created or updated by a user to require that a node's title, author, language, and CCK fields are unique within a given context.

I have not tested it yet, but it seems promising.

0

We can think of two coding approaches:

  1. Use hook_node_validate()

  2. Call '#validate' in form alter

We should not validate inside hook_node_insert() or, hook_node_presave()

You can make use of code like below:


  $type = 'Content_Type_Machine_Name_You_Want_To_Validate_For';

  if (YOUR_CONDITION_FOR_CONTENT_TYPE_CHECK) {  
    $query = db_select('node', 'n');
    $node_title_exists =  $query->fields('n', array('nid'))
      ->condition('n.type', $type, '=')
      ->condition('n.title', 'SUBMITTED_NODE_TITLE', '=')
      ->execute()
      ->fetchField();
    if ($node_title_exists) {
      // WRITE form_set_error();    
    }
  }

Hope it helps

0

What you are looking for is node title validation https://www.drupal.org/project/node_title_validation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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