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? :)

| improve this question | | | | |

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.

| improve this answer | | | | |

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';

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

Hope it helps

| improve this answer | | | | |

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

| improve this answer | | | | |

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.