Estoy escribiendo un módulo personalizado y lo necesito para cargar una imagen. Tengo problemas para encontrar buena documentación sobre esto, pero creo que estoy cerca.
¿Qué me estoy perdiendo? $ file devuelve false en el envío del formulario.
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.'));
}