Hi
I am creating nodes programmatically and giving titles to nodes indiviually, however it's causing duplicates the module can't see.

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

How should I relate 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

carlovdb’s picture

Does anyone has a solution?

oryx’s picture

It took me quite some time to figure this out, but you can use the unique_field_match_value() function to programatically check that your value is not already stored in a unique field. If the field value already exists, the function returns an array containing the nid the existing value is attached to.

Beware : it only works with nodes (not for custom entities), and the field used has to be defined as unique through the unique_field UI.

$field = your field machine name or node property (e.g. 'title')
$scope = 'node'
$ntype = node type (bundle)
$nlanguage = node language ('und' if undefined)

<?php
$values[]['value'] = "your_value"; //mandatory structure for this array, you can check for multiple values at the same time
$match = unique_field_match_value($field, $values, $scope, $ntype, $nlanguage);
if (isset($match) && !empty($match) { do someting... }
?>
oryx’s picture

Status: Active » Fixed

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