0

how to update a field value of one content type node when a field value of another content type is changed using hook_node_presave?

for eg: content type | field name | field value | updated value

       product      | product_stock |      10     |     24
       Add stock    | stock value   |      14     | 
2
  • sorry for the example. i mean to say: content type field name field value updated value product product_stock 10 24 Add stock stock value 14
    – aashi
    Commented Jan 22, 2013 at 6:26
  • sorry for the example. i mean to say:
    – aashi
    Commented Jan 22, 2013 at 6:26

1 Answer 1

0
  • First of all you need to link the two nodes - check out this module - http://drupal.org/project/references. It provides a field (add to one content type) that stores reference (of another reference type) ie. the nid
  • So 2 nodes will now be connected.
  • Next, in your hook_node_update check when node of the first content type is updated - get the nid of the related node from its node reference field => node_load(nid) => change whatever value you want => node_save(nid).

Hope this wokrs for you. All the best!

9
  • how to get get the nid of the related node from its node reference field. Plz reply. I think it will work
    – aashi
    Commented Jan 22, 2013 at 8:49
  • function my_module_node_presave($node){ if($node->type == 'add_stock') { $product = $node->field_stock_value['und'][0]['value']; drupal_set_message($product); $addStock = $node->field_reference['und'][0]['nid']; $p_node = node_load($addStock); drupal_set_message($p_node); } }
    – aashi
    Commented Jan 22, 2013 at 9:06
  • i had done this, written above. I had created a custom module my_module and inside that my_module_node_presave. But it gives an fatal error.
    – aashi
    Commented Jan 22, 2013 at 9:08
  • If you don't know laready then first off all install drupal.org/project/devel. Then you can check your node object(or anything in any function) using dsm($node). In there you will find the nid from node reference field. Hoe far have you got till now?
    – SGhosh
    Commented Jan 22, 2013 at 10:34
  • And I think hook_node_update might(I say might...not sure) be the place to do this.
    – SGhosh
    Commented Jan 22, 2013 at 10:40

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.