Fill a non-computed field
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_name
I also have a content type with two text fields:
field_id
field_firstname
If 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'):
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 and tags.
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.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion