3

I programmed a multi-step form using the drupal form API. On the first page is a user image file upload using the drupal managed field managed_file. When the form is stepped thru to the end and submitted, the already in the single steps validated data will be stored into profile2, by creating a profile2 object with profile_create and set the assign the fields. This all works without problem. But how can I store the FID of the uploaded file to the profile2 user_image field? A s I saw in mysql, the file FID and the measurements of the image are stored in the field table.

I have this (file_load code snippet is untested) in the form submit step, where the data gets stored into profile2:

$profile = profile_create(array
    'user' => $new_user,
    'type' => 'main'));
$profile->field_firstname['und'][0]['value'] = $user_data_collection['t_1_1'];
// ... 
if (isset($user_data_collection['t_1_22'])) {
    $file = file_load($user_data_collection['t_1_22']);
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);

    $fid = $file->fid;

    // --- how can I store the FID, x and y measurements of the image?
    // ...
}
// ...   
profile2_save($profile);

Can somebody give me a hint? I was searching on google but did not find an answer to my question. Thanks in advance!

Update: This is how I solved it now. Since I work with an "managed_file" Field in the form, and later for displaying the image file via the theme() fct I do not have to care about resizing. This is done due to the image cache / theme fct's with a properly defined image-style.

$profile = profile_create(array(
   'user' => $new_user,
   'type' => 'main'));

//...

if (isset($user_data_collection['t_1_22'])) {
   $file = file_load($user_data_collection['t_1_22']);
   // --- Flag that this file is permanent and should not be deleted.
   $file->status = FILE_STATUS_PERMANENT;
   // --- save file object to the database
   file_save($file);

   // --- get original x/y measurements for this image
   $img_info = image_get_info(drupal_realpath($file->uri));
   // --- store fid. x and y to profile 2
   $profile->field_image_user[
      'und'][0]['fid'] = $file->fid;
   $profile->field_image_user[
      'und'][0]['width'] = $img_info['width'];
   $profile->field_image_user[
      'und'][0]['height'] = $img_info['height'];
}

//...

// --- save profile data
profile2_save($profile);

So all seems to work and have the solution that I needed.

3

I think you are asking how to save user picture. Users table holds the place for picture.

$account = user_load($new_user->uid);
user_save($account, array('picture' => $fid));

EDIT

Profile2 module is based on entities using Entity API module. You can use entity_metadata_wrapper('profile2', $profile->pid) to get a metadata object. This object will hold the properties (native profile2 fields) and Fields (additional properties plugged in via field api or Field Admin UI)

$profile = profile2_load_by_user($account, 'main');
$profile_w = entity_metadata_wrapper('profile2', $profile->pid);
$proflie_w->field_myfield->value(); // This is a read operation
$proflie_w->field_myfield->set('new value'); // This is write operation
$proflie_w->save(); // Finaly save the things

You can bypass metadata wrapper.

$profile = profile2_load_by_user($account, 'main'); // Loading existing profile instance
OR
$profile = profile2_create(array('user' => $account, 'type' => 'main')); // Providing a new profile instance
$profile->field_myfield = "new value";
$profile->save();

NOTE: you can check all your listed properties using foreach operation on the wrapper object. e.g.

$profile_w = entity_metadata_wrapper('profile2', $profile->pid);
foreach($profile_w as $key => $obj){
   debug($key) // give you name of your property
  • I keep an extended user profile with about 20 fields like pers. interest, work exp., education and all the likes. In that extended profile is also the user picture and a company logo stored. I've written a profile edit page (is the extra page to the user account data). All works great there. The form I programmed above is combined with the user registration, I need to store the picture in the profile2 data fields along with the other profile data. I am just confused because besides the FID are also the measurements of the picture stored in the mysql field table. There should be a way to do so. – Andreas W. Wylach Jun 21 '12 at 15:31
  • Yes, I had the feeling, that I will have to respect the entity API somehow. So I think the way to go is to use the first code snippet. Question is, I create a new profile with profile_create(). So using the entity_metadata_wrapper() fct, is there already a pid availble in the returned object? Because in my App at that point I store a new profile2 object. See my snippet in the question. Maybe I have to call entity_metadata_wrapper('profile2', $profile), with the profile object instead of the object field pid? Then I would have to store the single values of the image field (x, y, fid)?! – Andreas W. Wylach Jun 22 '12 at 2:02
  • You can skip the loading process. Focus on second snippet block. Do not load it as you are creating a new one. Directly manipulate the field_xyz properties of profile object and then call $profile->save(); this is it :) – Shoaib Nawaz Jun 22 '12 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.