Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I have created a computed field that modify a cck field according to the title of the node:
$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
Comment #1
colanComment #2
luthien CreditAttribution: luthien commentedI 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.
Comment #3
ciss CreditAttribution: ciss commentedAll you have to do is to check if the field already contains a 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.
Comment #4
nikikelly74 CreditAttribution: nikikelly74 commentedAnyone 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.
Comment #5
ciss CreditAttribution: ciss commentedTwo changes are required:
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.
Comment #6
nikikelly74 CreditAttribution: nikikelly74 commentedI 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;
}
Comment #7
nikikelly74 CreditAttribution: nikikelly74 commentedThanks 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.
Comment #8
luthien CreditAttribution: luthien commentedHi, 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.