Estou escrevendo um módulo personalizado e preciso fazer o upload de uma imagem. Estou tendo problemas para encontrar boa documentação sobre isso, mas acho que estou perto.
o que estou perdendo? $ file retorna false no envio do formulário.
function mymodule_custom_content_block_form($form_state){
$form = array();
$form['custom_content_block_text'] = array(
'#type' => 'textarea',
'#title' => t('Block text'),
'#default_value' => variable_get('mymodule_custom_content_block_text'),
'#required' => true,
);
$form['custom_content_block_image'] = array(
'#type' => 'file',
'#name' => 'custom_content_block_image',
'#title' => t('Block image'),
'#size' => 40,
'#description' => t("Image should be less than 400 pixels wide and in JPG format."),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Update'),
);
return $form;
}
function mymodule_custom_content_block_form_submit($form, &$form_state){
if(isset($form_state['values']['custom_content_block_image'])){
$validators = array('file_validate_extensions' => array('jpg jpeg'));
$file = file_save_upload('custom_content_block_image', $validators, 'public://');
if($file == false){
drupal_set_message(t("Error saving image."), $type = "error", $repeat = false);
}
else{
$file->status = FILE_STATUS_PERMANENT;
$file = file_save($file);
}
}
variable_set('mymodule_custom_content_block_text', $form_state['values']['custom_content_block_text']);
drupal_set_message(t('Custom Content Block has been updated.'));
}