0

I am trying to programmatically update a node with a value from another entity. When I call node_save, the function just "stops". There is nothing in watchdog or in my php_logs that would indicate that any issue was encountered.

$node = node_load($base_node->nid, FALSE, TRUE);

$node->field_product[LANGUAGE_NONE][] = array('product_id' => $entity->product_id->value());

dpm('This text prints.');

node_save($node);

dpm('This text does not print.'); //This line is never executed.

I have tried a variety of methods for saving the node including using entitymetadatawrappers on $node. So far, nothing I have tried has gotten me anywhere.

Thanks!

Edit:

A few more details on what I have tried/am trying to do.

This field is a product reference field that I am adding a reference to.

There is nothing at all in the php logs or the watchdog logs.

I am trying to do this as a part of custom rules action. I was originally using the add item to list action, but only one item was being added to the list (usually there will be 4-5). As a test, I tried to update and save the node manually (thus the code above).

I should have specified that part, but got a bit wrapped up in my own solution. Either way, it seems pretty odd to me.

Edit Part 2:

I have confirmed that the structure for field_product is correct. I am also doing checks to make sure that I am not referencing a non-existent field, etc.

I wasn't originally using dpms for debugging, it just seemed like the simplest way to communicate my issue. Originally I was doing some further processing on my data that was not being done. Since that further logic isn't pertinent to the problem, I added a dpm as an example. There were no errors in the following logic and it wasn't dependent on the node save.

  • Do you get a WSOD? Did you check you server logs for PHP errors? – cilefen Nov 30 '15 at 20:32
  • I don't get a WSOD. Everything continues as though nothing went wrong. There is nothing in the logs. – lukedekker Nov 30 '15 at 20:43
  • Please try to use dsm($node->field_product); to display the content and structure of that variable. Check if the structure of the value you're assigning is the same. Where are you executing this code also? Thanks! – Leolando Tan Nov 30 '15 at 21:22
  • I have done that, and the format is the same. This code is part of a custom rules action. – lukedekker Nov 30 '15 at 21:33
  • 1
    by doing $node->field_product[LANGUAGE_NONE][] you are implying that $node->field_product[LANGUAGE_NONE][1] will be created if [0] already exists. Moreover are you sure it is 'product_id' and not 'value'. Last dpm may not be the best way to debug a rules action. use watchdog('debug', '<pre>'.print_r($var,true).'</pre>'); instead and make sure the action is triggered by enabling rules debug in advanced options. – Nikos Tsagkas Nov 30 '15 at 22:01
0

The field structure you are using for your entity reference field is not correct. See entityreference_field_schema().

The correct column name is target_id, not product_id, so it should be like this:

$node = node_load($base_node->nid, FALSE, TRUE);

$node->field_product[LANGUAGE_NONE][]['target_id'] = $entity->product_id->value();

node_save($node);

This assumes you are using an entity wrapper to access the $entity object but not for accessing the $node object.

[edit]

Regarding your dpm() not firing, it is quite possible it is firing but you aren't seeing the output. This can happen depending on where you are calling it from.

A good alternative in these cases could be dd() or watchdog() since their output will be visible regardless of where you call it from.

  • I got my wires crossed. This is a product reference field, not an entity reference field. For a product reference field product_id is the correct column name. I have tried alternatives todpm() and it isn't making a difference. I've updated the post to try to provide some additional clarity – lukedekker Nov 30 '15 at 23:23

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.