Hello,

What I want to achieve is to create nodes automatically. But when the title already exists, the node should not be made.

I have achieved to let drupal create the node, but i am stuck with the title check. Can someone help me a bit?

I have created a rule that executes a PHP-code after a cron update. The code that I have now is:

//Give all the titles from the content type 'betaling'
//How do I do this?

//Search for pattern in the string
$pattern = "/(?<=\FACT-)(.*?)(?=\-KAHO)/";
$subject = "Dit is een omschrijvingsveld FACT-0045/2012-KAHO, maar dit is ook FACT-0032/2012-KAHO en FACT-0015/2012-KAHO";
preg_match_all($pattern, $subject, $matches);
while (list($key, $value) = each($matches[1])) {
  $body_text = 'This is the body text I want entered with the node.';
 
  $node = new stdClass();
  $node->type = 'betaling';
  node_object_prepare($node);
 
  $node->title    = "FACT-".$value."-KAHO";
  $node->language = LANGUAGE_NONE;

  $node->body[$node->language][0]['value']   = $body_text;
  $node->body[$node->language][0]['summary'] = text_summary($body_text);
  $node->body[$node->language][0]['format']  = 'filtered_html';

  $path = 'content/programmatically_created_node_' . date('YmdHis');
  $node->path = array('alias' => $path);

  node_save($node);

}

Comments

systemick’s picture

Can't you just query the database to see if the title exists and then act accordingly:

Add the function

function mymodule_node_title_exists($title) {
  return db_query("SELECT nid FROM {node} WHERE title = :title", array(':title' => $title))->fetchField();
}

Then add the line

if (!mymodule_node_title_exists($node->title)) {
  node_save($node);
}

to the code above.
carlovdb’s picture

Thank you very much..
I was also trying to query the database, but it did not work correctly, but with your code above, it works like a charm.. !!
thank you