Last updated May 6, 2009. Created by lelizondo on May 6, 2009.
Log in to edit this page.
Have you ever had a a case when you have a field that you want to be filled with data from other sources but you also want the field to be a text field in case the user wants to fill it with a different value? The next snippet does that.
Using an example will make things easier. I have a case where I have a table with members, two columns:
id
first_nameI also have a content type with two text fields:
field_id
field_firstnameIf a user fills up the field_id text field, then I want the form to look up in the table 'members' to see it that ID exists, if it does exists, I already know the value for field_firstname so I want the field_firstname to be filled with the data.
But, if the user wants to fill up the field_firstname, this should be possible.
First, you'll have to create two fields, the field_id and the field_firstname, but also you have to create a third field, a computed field, I'm gonna call it 'field_operations' since it's only going to perform the operations to fill the field_firstname.
Now the code for 'field_operations' (remember, you don't have to do anything with field_id and field_firstname, all the code goes into 'field_operations'):
<?php
if (!empty($node->field_firstname[0]['value'])) {
// the field 'field_firstname' has a value, I don't actually care if the user filled the field or if it was generated by computed field, all I care about is the field has a value and I want to keep it.
return $node->field_firstname[0]['value'];
}
else {
// if the field it's empty then we fill the field with the data from the table 'members', also this code doesn't cover the case where the ID was changed by the user on node edit, if that's the case we have to add more code, but that's out of the scope of this tutorial.
$id = $node->field_id['0']['value'];
// we get the value from field_id, we are going to use it to query the database.
$result = db_fetch_array(db_query("SELECT first_name FROM {members} WHERE id = %d", $id));
// we order the database to look for the value of 'first_name' (not the field) where the id it's equal to $id, the value the user entered in field_id
// $node_field[0]['value'] = $result[first_name];
// this result will save the value in the field field_operations. we don't want this, this is what we usually do, we want the value to be stored in the field field_firstname
$node->field_firstname[0]['value'] = $result[first_name];
// finally we save the value from our query into the field_firstname.
}
?>
Remember to remove the
<?php
and
?>What's next?
You can save the field to the database if you want, just remember to hide it since you'll have two fields with the same values on node view.
Also, it would be so nice if we could fill the field_firstname with AJAX so the user knows the form found a valid firstname for the given ID. I don't really know how to do it, if someone has more experience with AJAX, please edit and post.
Comments
There's a small issue with
There's a small issue with this. The fields computed field will fill automatically can't be required, apparently cck validation for required fields runs first. Don't know how to fix this.
Luis
I tried something simmilar
Ok computing is working right way...
But I can't get some variables to populate not computing fields... I set example field_insert_mini_image this way:
$node->field_insert_mini_image[0]['value'] = $str1;But the field is empty after preview/sugmit...
hello drupal world!
my drupal site: Reprodukcje, obrazy
Ok it's little bug.
Just after preview button clicked fields are not refreshed. Bu after save/submit pagel looks like it should... I thought preview and submit shoud do the same...
hello drupal world!
my drupal site: Reprodukcje, obrazy
I think the problem is
I think the problem is somehow related to computed field, since it doesn't perform the operations until the node gets saved. But I don't think this is a bug, it's just the way the module works. Why? Don't know the answer, but it was designed to work that way.
Luis
This took me about 2 hours to
This took me about 2 hours to work out why it's not working when clicking preview... Than I just tried "submit". These were stressfull hours as I thought: "hey something is wrgong with me or with this module" :-) Looking around some settings a few times if I don't miss somethins, read manual few times, checked my php code few times...
It's good to know about such features - takes less time to solve the problem:)
hello drupal world!
my drupal site: Reprodukcje, obrazy
Only works sometimes
I tried this to set a value of a node reference type field of a content profile type. Sometimes the filed set to proper value, but sometimes it is not (null). Any idea why is that?
//get user points
$the_user = user_load($node->uid);
$points = userpoints_get_current_points($the_user->uid, 'all');
//decide user badge
if(in_array('moderator' ,$the_user->roles) ){
$card = 39073;
} else {
if($points > 100000){
$card = 39072;
} elseif ($points > 50000) {
$card = 39070;
} elseif ($points > 10000) {
$card = 39071;
} else {
$card = 39069;
}
}
$node_field[0]['value'] = $card;
$node->field_member_card[0]['nid']= $card; //set node reference field
d7 - It's like my function isn't even being called
I had code that tried to do this, but nothing showed up the first few times. I figured it was because of bad code (understandable), so I just put a simple number as the value just to test that the function was being called at all. I know the code is executed at node_save() but going to Edit, then saving the node, still doesn't even show a simple 5 as the output. Also, my display is set to "raw computed value", and it's not hidden, my data is set to save as a varchar, and to be saved in the db because I need it for a view later. I do have the field on my content type (through the UI, but that shouldn't matter), and it's named durationtest (so, field_durationtest). This makes no sense to me, and I've been googling like a crazy woman trying to figure it out. Any tips or anything would be much appreciated. Thanks.
This is my simple simple computed field function:
<?phpfunction computed_field_field_durationtest_compute(){
$entity_field[0]['value']="5";
}
?>
Nothing shows up on node view. I've tried everything I could think of. I just know it's something stupid and simple, so I'm sorry in advance for posting, which I try to avoid.