If you have been using the stock profile module distributed with Drupal, it's time to switch to CCK to get more flexibility and control in theming and functionality.
There is an excellent tutorial on how to do just this (http://dev.shellmultimedia.com/node/55), but I already have hundreds of users. Here's how I salvaged the data.
Step 1: Create a User Profile node for each user
I noticed that the usernode module automatically creates a usernode per user on installation, so I put a couple of functions together in my site module based on the usernode code. (For those who don't know what a site module is, it's a module with functions specific to the site where you can put all sorts of functionality that will only be used for a particular site, or that I was too lazy to abstract out into stand-alone modules).
<?php
define('UPROFILE_CONTENT_TYPE', "uprofile");
/**
* Check all users for an existing uprofile, and create one if necessary.
*/
function custom_uprofile_check_all($limit = 100) {
// create usernodes for all existing users without a usernode
$result = db_query_range(
"SELECT u2.* FROM {users} u2 ".
"WHERE u2.uid != 0 ".
"AND u2.uid NOT IN (".
"SELECT u.uid FROM users u ".
"LEFT JOIN node n ON u.uid = n.uid ".
"WHERE n.type = 'uprofile' AND u.uid != 0)",
0, $limit);
$processed = 0;
while ($user = db_fetch_object($result)) {
wtt_uprofile_create_node($user);
$processed ++;
}
return FALSE;
}
/**
* Create an associated node. Called by hook_user().
*/
function custom_uprofile_create_node($user) {
$node->uid = $user->uid;
$node->type = UPROFILE_CONTENT_TYPE;
$node->title = 'User Profile';
$node->status = 1;
// workaround to disable drupal message "Your usernode has been created"
$messages = drupal_get_messages();
// create the usernode
node_save($node);
// write back the old messages
$_SESSION['messages'] = $messages;
}
?>Now run custom_uprofile_check_all(); in your "Execute php" box courtesy of the Devel module until all of your users have a user profile node.
Note that the $limit parameter in custom_uprofile_check_all() only creates 100 at a time, so either bump that up or run that function more times as needed.
Step 2: Create a CCK record for each User Profile node
If you follow the Shell Multimedia tutorial, the User Profile content type has its own database table called "content_type_uprofile".
Just run the following SQL script:
INSERT INTO `content_type_uprofile` (nid, vid)
SELECT nid, vid
FROM `node`
WHERE
type = 'uprofile'...and a record is inserted for each corresponding record of type "uprofile" in your node table.
Step 3: Create the User Profile Fields
Instead of using the import data from the Shell Multimedia tutorial, I decided to create my own to suit the user data already stored using the stock profile module. For example I added the following fields of type "text":
- First name becomes field_first_name_value
- Last name becomes field_last_name_value
Step 4: Populating User Profile data
My users had already filled out their first names using the stock profile module, so I hunted down the old profile data in database table "profile_fields", and found that the fid corresponding to First name equals 8. So I ran the SQL script:
UPDATE `content_type_uprofile` AS ctu
LEFT JOIN node AS n
ON n.nid = ctu.nid
AND n.type = 'uprofile'
SET `field_who_you_are_value` = (
SELECT pv.value
FROM `profile_values` AS pv
WHERE n.uid = pv.uid
AND pv.fid = 8
);...and voila, First names are filled into "content_type_uprofile"!
Now repeat with other fields.
More to follow...
