Adding imagefields to user profile form
Setup is following:
Content profile module for having a content type linked with user profiles.
On this content type I have imagefield allowing multiple images, added via CCK.
And custom module that automatically creates profile node for user on register and changes user editing form to have imagefield showing up there.
I assign fields on profile form like this: (code from hook_user with $op = 'form' and $category = 'photos fieldset')
//getting CCK field with photo from profile node
module_load_include('inc', 'node', 'node.pages');
module_load_include('inc', 'filefield', 'filefield_widget');
module_load_include('module', 'imagefield');
$result = db_query('SELECT nid from {node} WHERE uid = %d AND type = "profile"',$account->uid);
$nid = (int) db_result($result);
$node = node_load($nid);
$dummy_formstate = array();
$profile_form = drupal_retrieve_form('profile_node_form',$dummy_formstate,$node);
drupal_prepare_form('profile_node_form',$profile_form,$dummy_formstate);
$form['photos fieldset']['field_profile_photos'] = $profile_form['field_profile_photos'];
// ^^ photos fieldset created in /admin/user/profile and have other fields
//fix for correct saving of files:
$form['type'] = array('#value'=>'profile','#type'=>'hidden');
And it is displaying fine. In hook_user I have dummy case for $op = 'insert' and $category = 'photos fieldset':
unset($edit['field_profile_photos']);
File gets uploaded with AJAX just fine — it appears on form and in filesystem.
But when I submit form I get following errors:
warning: md5() expects parameter 1 to be string, array given in /mnt/win_s/work/drupal_hdc/modules/user/user.module on line 225.
user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0 = '', data = 'a:7:{s:13:\"form_build_id\";s:37:\"form-0059af5cb8f402ae3578cbf0' at line 1 query: UPDATE users SET 0 = '', data = 'a:7:{s:13:\"form_build_id\";s:37:\"form-0059af5cb8f402ae3578cbf0878be786\";s:4:\"type\";s:7:\"profile\";s:3:\"fid\";s:3:\"126\";s:4:\"list\";s:1:\"1\";s:6:\"upload\";s:0:\"\";s:10:\"upload_btn\";s:6:\"Upload\";s:10:\"remove_btn\";s:6:\"Remove\";}' WHERE uid = 42 in /mnt/win_s/work/drupal_hdc/modules/user/user.module on line 247.
Drupal's user_save function receives in second parameter array with fields and values to save. And filefield data is copied there with key 0 causing that error.
When I edit node and upload/remove pictures there — all goes fine.
The question is: what am I doing wrong, and is it possible at all to have imagefield working not on CCK node-editing forms.
PS, version info:
Drupal: 6.4
CCK: 6.x-2.0-rc6
filefield: 6.x-3.0-alpha4
imagefield: 6.x-3.0-alpha2
Comments
Comment #1
drewish commentedyou should be using something like the bio or user profile module.
Comment #2
pzskc383 commenteduser profile module is used right now for most of text fields, that need to be in profile. but it's not possible to add filefields on them directly, so I have decided to take this approach as only possible.
Comment #3
drewish commentedlook at bio module. you shouldn't be using the core profile module for this...
Comment #4
steinmb commentedThere is a article on Lullabot http://www.lullabot.com/articles/imagecache_example_user_profile_pictures