Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hello,
I am programmatically creating nodes, with this piece of code
.....
$node = node_submit($node);
if ($node->validated)
{
node_save($node);
echo 'Node: ' . $node->title . '(' . $node->nid . ') was saved. <br />';
}
else
{
echo 'Node: ' . $node->title . '(' . $node->nid . ') was not saved. <br />';
}
...
Duplicate nodes are saved without a problem, even though I have Unique field installed. Once I go to edit a node like this in the browser, it does show that uniquness check failed and does not let save.
Comments
Comment #1
arithmetric CreditAttribution: arithmetric commentedHi giorgio79,
The node_submit() function does not validate the node. As you can see from the node_submit() code, $node->validated is always set to TRUE:
http://api.drupal.org/api/function/node_submit/6
Instead, to validate a node you should use node_validate(). This will run the node's values through Unique Field's validation routine, as well as the node validation rules of Drupal core and any other modules. If validation fails, then a form error is set, which you can detect using form_get_errors().
If you want only the results from Unique Field's validation, then you could use module_invoke() to run only Unique Field's hook_nodeapi routine, and again check for errors using form_get_errors().
Hope that helps. Let me know if there is any trouble using this approach.
Comment #2
giorgio79 CreditAttribution: giorgio79 commentedWow, thanks Joe for this in depth explanation. This was perfect. Thank you very much, will do as you say :)
Comment #3
ikeigenwijs CreditAttribution: ikeigenwijs commentedI found this really helpful, keeping track of this.