0

I have written inside a Computed Field's Computed Code section the following code:

<?php
//work if the node is new and there is no value for the field yet
if (empty($entity_field[0]['value'])) {

  do {
    //calculate a promotion code
    $new_promotion_code = strtoupper(substr(md5(rand(999, time())), 0, 5));

    //check db for conflicts
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle','gift')
      ->fieldCondition('field_gift_code', 'value', $new_promotion_code)
      ->addMetaData('account', user_load(1));
    $result = $query->count()->execute();
  } while($result > 0);

  //assign the field value
  $entity_field[0]['value'] = $new_promotion_code;

  //option#1
  //$entity->title[0]['value'] = $new_promotion_code;
  //option#2
  //$entity->title[LANGUAGE_NONE][0]['value'] = $new_promotion_code;

  //presave the node with attached fields (not sure if this is working)
  field_attach_presave($entity_type, $entity);
}
?>

This code works except the commented out section which includes two options for setting up same node's title. I am also not sure if field_attach_presave() works but I have no means to debug it and it just doesn't fail like field_attach_update().

Using Auto Entity Label is also a solution but the token ([node:field-gift-code:rawvalue] or [node:field-gift-code]) for this field just prints Array.

Any suggestions?

0

1 Answer 1

0

You can't do it all in the first box.

Use the Computed Code box to create a value for the field in $entity_field[0]['value'].

Then use the Display code box to do something with this value.

4
  • Thank you very much for the answer. Can I just put $entity->title[0]['value'] = $entity_field_item['value']; into the display box?
    – duru
    Commented Apr 3, 2013 at 23:32
  • Give it a try, but if you call drupal functions, you must do it in the Display code box.
    – Triskelion
    Commented Apr 3, 2013 at 23:41
  • Got a peaceful WSOD =)
    – duru
    Commented Apr 3, 2013 at 23:49
  • Just tried to evaluate PHP in Auto Entity Label pattern: <?php print $entity->field_gift_code[0]['value']; ?> gave nothing.
    – duru
    Commented Apr 3, 2013 at 23:54

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.