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?