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' => 'mail@mail.com',
    'status' => 0,
    'language' => 'en',
    'init' => 'mail@mail.com',
    '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).

我现在已经尝试了很多技巧并且在谷歌上搜索了很长时间,以至于我什至无法解释我拥有什么并且没有尝试过!

请提供任何帮助。

4

1 回答 1

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);
    $edit = array(
    'name' => 'name',
    'mail' => 'mail@mail.com',
    'status' => 0,
    'language' => 'en',
    'init' => 'mail@mail.com',
    'roles' => array(8 => 'Promoter'),
    'field_business_image' => array(        
        'und' => array(
            0 => array(
            'fid' => $file -> fid,
        ),
      ),
    ),
    );
user_save(NULL, $edit);
}

Drupal 所寻找的只是数组中的文件fid。不知道为什么我最初认为它必须比这更复杂。

于 2012-10-13T13:15:14.030 回答