Hello,
I created a module to provide a content type and several fields (text) for that content type.
The content type's purpose is to hold values that is parsed from a feed.
After creating the content type en fields the node is working good when I use the Fields UI to create a new node. I can enter en edit all data and all is desplayed properly.

However, when I create the node trough coding, the parsed data does not appear in the node object.
Strange thing is that the $node-> title and $node-> body, which are both also parsed from the feed, works good. Only the other field fail.

Here's the code:

function my_function_fill_nodes($id, $item_to_save, $web_page) {
  // code removed - XPAT retrieving node details
 
  $node = new stdClass();
  $node-> type = 'ysm';
  $node-> internal_id = $id; // TODO
  $node-> title = $object_title;
  $node->language = 'en';
  node_object_prepare($node);
  foreach (XXXXXXXXXXXXXXX) {  // parsing a DOM object
    switch(XXXXXXXXXXXXXX) {
      case 'A' :
        $node-> price[$node-> language][0]['value'] = $retrieved_value; 
        break;
      case 'B' :
        $node-> model[$node->language][0]['value'] = $retrieved_value; 
		break;

		// this goes on for 10 items
      default:

	    echo($retrieved_value . '| not matched<br />');
    }
  }
  // seperate code parsing the body text ($body_text)

  // Save all parsed info as a node
  $node->body[$node->language][0]['value']   = $body_text;
  $node->body[$node->language][0]['summary'] = text_summary($body_text);
  $node->body[$node->language][0]['format']  = 1;
  node_save($node);
}

I have already triede to exclude certain things. I have done several dumps of the $node Object, and it always appears correctly, so with the prsed data. But the field values doesn't find their way to the DB.
I have been looking at the code for several days now, so maybe someone can have a quick look for me, as different eyes sees different things,
Thanks!

Comments

nevets’s picture

You might consider using the Feeds module.

clivesj’s picture

I have experimented with the feeds module, unfortunately it is not suitable.
The pages i have to scrape are very chaotic. This requires several different XPaths on one page. Also some logic is involved.
Handling the Feed is not the problem, the scraped data are loaded perfectly in de Node Object. Maybe node_save($node) it not doing it's job properly?
Thanks

dotpex’s picture

Try to remove node_object_prepare($node);

clivesj’s picture

Thanks Dotpex,
But after removing node_prepare the node is not created anymore. I understood node_prepare is setting al kind of default properties like status, promote etc. Apparently the node will not be created if these are missing.

BUT:
I'm having some additional problems now like the menusystem not reckognising my menu call backs. Suspected a cache issue.
So I'm gonna re-install drupal and start with a clean install. The one I´m working with is an upgraded D6.
So guys, hold your fire and thanks for the help so far.
I will report back as soon as I have the new environment.

clivesj’s picture

It was a language issue (I think because the nodes are being created by Anonymous User)

changing
$node->language = 'en';
to
$node->language = 'und';
solved the problem.

Furthermore:
$node->body[$node->language][0]['format'] = 1;
should be
$node->body[$node->language][0]['format'] = 'filtered_html';

ogi’s picture

$node->language = 'en' is OK if languages are enabled and en is valid language code. What should be changed is using $node->body[LANGUAGE_NONE][] = array('value' => ..., 'format' => 'filtered_htm');