Hello
I am trying to run uniqueness to find out duplicates in creating nodes programmatically like:

  $node = new stdClass();
  $node->uid = 1;
  $node->type = 'page';
  $node->title = 'Unique title - no more with this title, please';
  node_save($node);   

How do I compare a string with the node title?

if ($my_string_title != $node->title){
}
else {
}

I don't know how to relate the nids.
Is there a function or hook In the module? I had a look at the code and couldn't get it.

With Regards
Jorge

Comments

bforchhammer’s picture

Hi Jorge,

The module does not provide a "similarity function" as such, but uses e.g. the core search module to find related content.

You can call the uniqueness_content function directly, which will give you an array of nodes with similar titles/content -- depending on the configured search mode.

$similar_nodes = uniquenes_content(array('title' => $my_string_title));
foreach ($similar_nodes as $result) {
  //$result['nid'];
  //$result['title'];
}
Yorgg’s picture

Hi bforchhammer,

Thank you for the quick reply.
I have configured the module for uniqueness in the page content type for simple node title search.
I understand this is the first programmatic approach to duplicates in Drupal 7.
PHP is not going inside the foreach and I'm not understanding the purpose of $result.

I have set $my_string_title with the title of the desired node to be verified but when I run the code again it is continuing to programmatically create another node with the same titles.
What about the nids and the title?

Kind Regards,
Jorge.

bforchhammer’s picture

As I said, the module only provides "similarity search", it does not prevent duplicates... that's something you have to do yourself. (Also, there may be other modules that can help you with that).

$similar_nodes contains a list of nodes which have a similar title to the one you have put into $my_string_title.
Foreach iterates over that list.

bforchhammer’s picture

Status: Active » Fixed

Closing this issue, please reopen if there's further questions.

Automatically closed -- issue fixed for 2 weeks with no activity.