Make node title unique in Drupal 7 (code snippet)

Last updated on
21 August 2023

If you want make a field unique, you could both use this module or Unique field.

If you want make a title unique, it is better to use Node Title Validation (or Unique field), but this could also be done through Field validation. So, if you want the article title to be unique, you could do it in this hacky way:

Add a PHP code validator to the body field, then add following code

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
$query->entityCondition('bundle', 'article');
$query->propertyCondition('title', $this->entity->title);
$flag = (bool)$query->range(0, 1)->count()->execute();
if($flag){
 form_set_error('title', $this->get_error_message());
}

A little hacky, but it works. You could use this method validate other properties such as uid, language, title ....

Help improve this page

Page status: Not set

You can: