0

I'm using menu_get_object() to get the NID for a computed field. The problem is that on a node save, NID doesn't exist yet so the computed field produces an error. If I resave the node, everything works since now the NID exists for the computed field to refer to. How can I go about adding an IF statement that if NID doesn't exist, save the node?

$node = menu_get_object();
$assigned_uid = $node->field_agent[LANGUAGE_NONE][0]['target_id'];
$assigned_user = user_load($assigned_uid);
$percentage_split = $assigned_user->field_percentage_split[LANGUAGE_NONE][0]['value'];

$other_ins = (isset(field_get_items($entity_type, $entity, 'field_other_ins_')[0]) ? field_get_items($entity_type, $entity, 'field_other_ins_')[0]['value'] : 0);
$vb = (isset(field_get_items($entity_type, $entity, 'field_vb')[0]) ? field_get_items($entity_type, $entity, 'field_vb')[0]['value'] : 0);
$finder_s_fee = (isset(field_get_items($entity_type, $entity, 'field_finder_s_fee')[0]) ? field_get_items($entity_type, $entity, 'field_finder_s_fee')[0]['value'] : 0);
$broker_fee = (isset(field_get_items($entity_type, $entity, 'field_broker_fee')[0]) ? field_get_items($entity_type, $entity, 'field_broker_fee')[0]['value'] : 0);
$dlc_franchise_fee_hst = (isset(field_get_items($entity_type, $entity, 'field_franchise_fee_hst')[0]) ? field_get_items($entity_type, $entity, 'field_franchise_fee_hst')[0]['value'] : 0);

$value = (($other_ins + $vb + $finder_s_fee + $broker_fee) - $dlc_franchise_fee_hst ) * ($percentage_split/100);
$entity_field[0]['value'] = $value;
1

Try this at the top of your code:

global $node;
if (empty($node->nid)) {
  node_save($node);
}

Worked for me in D5 and D6.

9
  • Thanks. I don't get an error but also nothing is computed, even on resave. – Ash Mar 15 '13 at 19:12
  • Try commenting out $node = menu_get_object();. – Triskelion Mar 15 '13 at 21:05
  • No error, but again without $node = menu_get_object();, assigned_uid doesn't know what $node is. – Ash Mar 15 '13 at 21:56
  • The replace global $node; with $node = menu_get_object; and see what happens. With the two in play, the question is which $node are you looking at! – Triskelion Mar 15 '13 at 22:30
  • Really appreciate the help. Now I get this error Missing bundle property on entity of type node. in entity_extract_ids() (line 7633... I think that the $node->nid part is the problem. Before the menu_get_object(); solution, we had quite a bit of trouble determining the NID. – Ash Mar 18 '13 at 12:21

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.