2

I have 2 content types: video and product. Each product contains a node reference to a video, and products can be linked to multiple videos. I'm writing a custom module to add in lists of these products and videos from an outside URL with JSON. Loading the content into Drupal isn't an issue.

What I want to do is check to see if the product exists, and if so then to add an additional node reference to the loaded video. The JSON spits out an array of videos, and there's a product array within each video array (so 1-10 products can be linked to one video). Each product has a specific product ID I'm passing in, and with that I'm able to single out the product node that needs to be updated. I load the product node, and then instead of adding an additional reference, the code I have is replacing the node reference. I've tried a handful of changes which either alter nothing or throw an error.

Here's the code:

 $result = $efq->entityCondition('entity_type', 'node')
  ->fieldCondition('field_product_id', 'value', $prodID, '=')
  ->execute();
 if (!empty($result['node'])) {
   $nids = array_keys($result['node']);
   $node = node_load($nids[0]);
   $node->field_video['und'][0]['target_id'] = $node_id;
   node_save($node);
 }
 else
 {
   Add product node
 }
2
  • The current problem / error is unclear Commented Jan 31, 2013 at 21:58
  • The code above replaced the current node reference. I need to keep the original node reference and add an additional one. The field is set to accept unlimited references, so that's not the issue. The issue is I don't know how to write that in the code.
    – cfox
    Commented Feb 1, 2013 at 13:15

1 Answer 1

0

Found a solution, which was replacing this line:

 $node->field_video['und'][0]['target_id'] = $node_id;

With:

 $new = array('target_id' => $node_id);
 $node->field_video['und'][0] = $new;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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