4

I'm creating nodes from XML using the Feeds module (great module BTW).

function boardgamegeek_menu() {
  $items['games/add/%/%'] = array(
    'title' => 'Boardgamegeek add game',
    'description' => 'Boardgamegeek add game',
    'page arguments' => array(2, 3),
    'page callback' => 'boardgamegeek_add',
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

function boardgamegeek_add($objectid, $title) {
  $node = new stdClass();
  $node->type = 'boardgame';
  $node->title = $title;
  $node->feeds['FeedsHTTPFetcher']['source'] = 'http://www.boardgamegeek.com/xmlapi/boardgame/' . $objectid;
  node_save($node);
  /*
  // This does an import
  $myFeed = feeds_source('game_importer');
  while (FEEDS_BATCH_COMPLETE != $myFeed->import());
  */
  return 'http://www.boardgamegeek.com/xmlapi/boardgame/' . $objectid;
}

When I run this code, a node is created with a title field value from $title (that's exactly what I want). The import doesn't start automatically, and when I start the import myself, another node is being created without any title.

I would like to update the node I created...

enter image description here The feed_nid and the entity_id should be the same. The feed_nid is the node id from the node I created, the entity id is the node id feeds created.

Any ideas?

  • Where is located that function you pasted here ? – Gregory Kapustin Jan 25 '13 at 16:51
  • In a custom module. I changed my code above. – screaming SiLENCE Jan 25 '13 at 16:55
  • 1
    And so your import script doesn't import after node_save ? – Gregory Kapustin Jan 25 '13 at 17:08
  • It doesn't import, and when I import myself (also in the code, but commented) another node is created. The node I created is not being updated. – screaming SiLENCE Jan 25 '13 at 22:44
  • 1
    In feeds mapping, the title field is declared unique, right? Because, once you create a node with a title, when feeds importer will run on a source(xml source in your case) with the same title, the already existing node with the same title will be updated. – SGhosh Jan 26 '13 at 6:44
4
+50

From the description what I understand is that you are trying to create a node with all the contents from the feed except the Title which you have set manually.

You have created a feed which would run when you create a node of the type "boardgame" with an HTTP Fetcher.

The way feeds work in this case is when you create the node with the attached feed. It creates a node for the feed itself and also creates additional nodes with the contents in the feed. So in effect the feed is not meant to update the node executing the feed but to create additional nodes from the feed itself. And I guess that is happening without any issue in your setup.

To achieve what ou want to do you can install the following module

Feeds Self Parser

What this does it to update the node being created from the feed it self and not create another node instead.

Note : You might have to clear cache for the module to work other wise you might get an error FeedsMissingPlugin_feeds_form in drupal_retrieve_form()

This way you dont really need to create a module just create a node with the with the title entered and the url as well. But if there is some other reason from creating the module then yes that would work fine as well.

Second option is to not use the feeds module itself. Just parse the xml manually and add the required fields and call the node_save() function. This would mean you wouldn't have to run the feed script after you create the node it would be done automatically by just calling the url. and it will also take you off the overheads of using the feeds module.

  • This seems like a very good answer, I'll dive into this and take a look at the two options you gave me. Parsing the XML with PHP is something that has benefits also, indeed. – screaming SiLENCE Jan 26 '13 at 10:03
  • Thanks mate, your answer was more then I wanted. I just needed a little push in the right direction...! – screaming SiLENCE Jan 26 '13 at 16:55
  • good to know that.... – Mohammed Shameem Jan 26 '13 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.