Php – Drupal 6: custom profile fields and saving data to a DB

drupalPHP

So i've added several some custom profile fields (administer -> profiles) which is all fine but completely useless if I can't store the information in a database. I've been searching for hours now trying to figure out the "best practices" way for doing this with very little luck.

Do I add the new columns to the Users table? Do I create a whole new table? I even found a vague reference that the core profile module should have added those columns for me anyway? And that you should be able to use CCK in the core profile module (but I don't have any options for that)?

And then I would obviously want to allow user to update their own fields but the the custom profile fields aren't included in the $form array…

PS arrrggg! Drupal is driving me around the bend with its inconsistencies and having to hack it all the time!

Best Answer

If you use the core profile module, the database storage is provided automatically and you will not need to modify anything in the database. On install, the module adds two tables to the database, profile_fields to store your custom field definitions and profile_values to store the user supplied data for those fields.

The fields are automatically added to the user edit forms, via the hook_user implementation in profile_user() - the same mechanism is used to add the values of those fields to the user object when that gets loaded.

So if those fields don't show up for you, something is fishy - have you added your fields with 'categories'? If so, they won't show on the standard user edit page, but on additional new pages (one per category). These are added as menu type MENU_LOCAL_TASK, so they should create new 'tab' entries at the top of the user edit page - maybe you have a theme that does not display the tabs?
Another thing to check would be the fields visibility settings chosen on the fields configuration form. If that is set to 'hidden', the field is only accessible for administrators and module/theme code. You should at least set it to 'private' if the user is supposed to edit it himself.

As for using CCK fields, I don't think that this is possible with the core profile module (maybe some extension module provides this). A different approach is taken by the Content Profile Module. It creates a custom node type for user profiles so that the profile values are stored as standard Drupal nodes. One advantage of this is that you can use all CCK fields for profiles, as you just need to add those to the created node type.

Related Topic