2012-10-05 126 views
0

我有一個創建一個新用戶,並在一些爲用戶自定義字段填寫的自定義表單。其中一個字段是自定義圖像(而不是系統頭像圖像)。如何自定義表單圖像保存爲自定義用戶字段

我可以上傳到通過表單服務器的形象,但不能讓它在適當的字段。這是我的(自定義模塊)代碼。

function newacc_freebusiness_form($form, &$form_state) { 

$form['bussimage'] = array(
    '#title' => t('Upload an image that shows off your business.'), 
    '#type' => 'managed_file', 
    '#description' => t('Max size of 3Mb and filetype of jpg jpeg or png'), 
    '#upload_location' => 'public://bussimages/', 
    '#upload_validators' => array(
    'file_validate_extensions' => array('png jpg jpeg'), 
    'file_validate_size' => array(3*1024*1024), 
    ), 
    '#required' => TRUE, 
); 
$form['submit'] = array(
    '#type' => 'submit', 
    '#value' => 'Submit', 
); 

$form['#validate'][] = 'newacc_freebusiness_validate'; 

return $form; 
} 
function newacc_freebusiness_validate($form, &$form_state) { 
    $bussimage = $form_state['values']['bussimage']; 
    $file = file_load($bussimage); 
    $bussimage = image_load($file -> uri); 
    image_save($bussimage); 
    $bussimage = image_load($file -> uri); 
    $edit = array(
    'name' => 'name', 
    'mail' => '[email protected]', 
    'status' => 0, 
    'language' => 'en', 
    'init' => '[email protected]', 
    'roles' => array(8 => 'Promoter'), 
    'field_business_image' => array(  
    'und' => array(
     0 => array(
      'value' => $bussimage, 
      ), 
     ), 
     ), 
     ); 
     user_save(NULL, $edit); 
} 

這是引發錯誤消息:

Notice: Undefined index: fid in file_field_presave() (line 219 of /var/www/drupal_site/modules/file/file.field.inc). 

我已經嘗試了這麼多花樣,現在和Google搜索這麼久,我甚至無法解釋我有什麼,並沒有再試過!

任何幫助,請。

+0

managed_file被提交-isn't之後保存到數據庫?所以你不能期望在驗證函數中使用文件對象的值。 –

+0

不,在單獨上傳驗證的表單驗證之前,託管文件會保存並存儲在數據庫中。 Image_load()返回包含fid的有效圖像對象。我只是不能得到user_save()將其存儲到我的領域。 –

+0

這是我在SO上的第一個問題,有人認爲有必要標記它?我認爲這很清楚,研究和幫助。無論如何 - 生活和學習。 –

回答

0

OK - 解決了這個。線索出現在錯誤信息中,解決方案非常簡單!這裏是代碼:

function newacc_freebusiness_form($form, &$form_state) { 

    $form['bussimage'] = array(
    '#title' => t('Upload an image that shows off your business.'), 
    '#type' => 'managed_file', 
    '#description' => t('Max size of 3Mb and filetype of jpg jpeg or png'), 
    '#upload_location' => 'public://bussimages/', 
    '#upload_validators' => array(
    'file_validate_extensions' => array('png jpg jpeg'), 
    'file_validate_size' => array(3*1024*1024), 
    ), 
    '#required' => TRUE, 
    ); 
$form['submit'] = array(
    '#type' => 'submit', 
    '#value' => 'Submit', 
); 

$form['#validate'][] = 'newacc_freebusiness_validate'; 

return $form; 
} 
function newacc_freebusiness_validate($form, &$form_state) { 
    $bussimage = $form_state['values']['bussimage']; 
    $file = file_load($bussimage); 
    $edit = array(
    'name' => 'name', 
    'mail' => '[email protected]', 
    'status' => 0, 
    'language' => 'en', 
    'init' => '[email protected]', 
    'roles' => array(8 => 'Promoter'), 
    'field_business_image' => array(  
     'und' => array(
      0 => array(
      'fid' => $file -> fid, 
     ), 
    ), 
    ), 
    ); 
user_save(NULL, $edit); 
} 

所有的Drupal尋找的是文件fid在數組中。不知道爲什麼我最初認爲它必須比這更復雜。