I have created a computed field that modify a cck field according to the title of the node:

<?php
$entity
->field_url_fueld[$entity->language][0]['value'] = $entity->title;
?>

I want it to generate only on time and the next time the user will change the title of the node it wont happen.
How can I do it?

Comments

Category:task» support

I need similar functionality. My computed field is a string created from concatenating two other fields and a random number. Every time I edit the node, the computed field changes. Having the option to generate it only once will be great.

All you have to do is to check if the field already contains a value:

<?php
if(empty($entity_field[0]['value']))
   
$entity_field[0]['value'] = 'my new value';
?>

I haven't checked for the initial value - you could probably use is_null() instead of empty().

Edit: is_null() can be used instead.

Anyone else have any insight to this problem. I am trying to generate a random ID when this particular content type is created and then leave it "as is" if the content is edited later on. So, create a random number initially and then pull that random number from the database every other time the content is edited.

I've tried this so many different ways, my head is spinning....

if (is_null($entity_field[0]['value'])); {
$pin = rand(1000000,2000000);
$numArray = explode(" ", $pin);
$arraySum = array_sum($numArray);
$remainder = $arraySum % 9;
$difference = 9 - $remainder;
$entity_field[0]['value'] = $pin.$difference;
}
else {
$entity_field[0]['value'] = field_patient_randomization_3_value[0]['value'];
}

My problem is that the record for the "computed field" is not even being created now. If I get rid of the If statement and just keep the random number generator, it adds a record then. I know I'm staring at something glaringly wrong. Just need another set of eyes on it.

Appreciate any help in advance.

Two changes are required:

  1. Remove the semicolon after your IF statement, otherwise the following block will always execute.
  2. Drop the else block, there's no need to reassign the value after you have assigned it once.

By the way, you probably overdid it a bit with your ID generation code. $pin will never contain a whitespace, so there's no need to explode on it (you probably wanted to split on ""). If you want unique values, either include the node ID if available or take a look at Serial Field.

I was so excited that it was such a stupid mistake with a semicolon, but it's still not posting a record in the database for that field after I remove it from the IF line.

My computed field (field_patient_randomization in attempt #1 and field_patient_randomization_3 in attempt #2) is to either be created when a new piece of content is created or to be pulled from the database when the content is edited so it doesn't change from its original value. Here are the two attempts I have going

Attempt #1

$asdf = field_patient_randomization_value[0]['value'];

$pin = rand(1000000,2000000);
$numArray = explode(" ", $pin);
$arraySum = array_sum($numArray);
$remainder = $arraySum % 9;
$difference = 9 - $remainder;

if (empty($asdf)) {
$entity_field[0]['value'] = $pin.$difference;
}
else {
$entity_field[0]['value'] = $asdf;
}

Attempt # 2

$entity_field[0]['value'] = field_patient_randomization_3_value[0]['value'];

if (is_null($entity_field[0]['value'])) {
$pin = rand(1000000,2000000);
$numArray = explode(" ", $pin);
$arraySum = array_sum($numArray);
$remainder = $arraySum % 9;
$difference = 9 - $remainder;
$entity_field[0]['value'] = $pin.$difference;
}

Thanks so much for your quick advice. I did remove the semicolon but it doesn't seem to have helped. It's such a simple if statement - it's making me crazy. Next, I'll simplify the random number generator as you advised....if I could only get this to work first - churning out a random id when the content is created initially and leaving it "as is" when the content is edited.

Hi, the code below worked out for me. My computed field is part of profile2 but it probably works the same for your regular content type.

if (!empty($entity_field[0]['value'])) {  // The PIN has already been generated, do nothing
return $entity_field[0]['value'];
}
else {  // Time to create a PIN. Generate a random number,  calculate a check digit via modulus-9, and append the check digit to the generated number
$pin = rand(10000,20000);
$numArray = explode(" ", $pin);
$arraySum = array_sum($numArray);
$remainder = $arraySum % 9;
$difference = 9 - $remainder;
$first_name = ucfirst(strtolower($entity-> field_first_name[LANGUAGE_NONE][0]['value']{0}));
$last_name = ucfirst(strtolower($entity-> field_last_name[LANGUAGE_NONE][0]['value']{0}));
$entity_field[0]['value'] = $last_name . $first_name . $pin.$difference;
}