drupal form api file upload - forms

I have a form with file attachment. Once i load the file and submit the form, if validation errors occur the form is loaded again, but the file i had uploaded is not rendered and I have to load it again.
I tried using file_save_upload but it doesnot seem to work.
$file_attach_set= file_save_upload('file_attachment1', array());
//$file_attach_setII = $form_state['values']['attc'];
$contextid = 150;
if(empty($file_attach_setII)){
$form['file_attachment' . $i] = array(
'#type' => 'file',
"#title" =>'kik'
'#default_value'=> $file_attach_set->fid,
//'#title_display' => $file_attach_set->uri,
''
);
}

The file element doesn't have a #default_value property. Try using the managed_file type which has this property.
https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#file
$form['file_attachment' . $i] = array(
'#type' => 'managed_file',
'#title' =>'kik',
'#default_value'=> array(
'fid' => $file_attach_set->fid,
),
);

Related

Additional Form submit button with redirection

I have added additional submit button on node create page via form_alter:
$form['check_data'] = array(
'#type' => 'submit',
'#access' => TRUE,
'#value' => 'Check',
'#validate' => array('node_form_validate'),
'#submit' => array('node_form_submit', 'custom_redirect'),
);
So the button now validates the form and saves the node, but it doesn't redirect it afterwards:
function custom_redirect($form, &$form_state) {
$form_state['redirect'] = 'node/%nid/check';
}
Any ideas how to redirect it after submission?
in _form_alter add the following:
$form['actions']['check_data']['#submit'][] = 'custom_redirect';
and/or check does form_state have nid and use it from there, like
$form_state['nid'], $form_state['redirect'] = 'node/' . $form_state['nid'] . '/check';

Drupal 7 | Form managed file upload image preview

I'm can't get to work image preview on Drupal 7 Form managed file.
I have for code like this in template.php:
function testform($form, &$form_state) {
$form = array();
$form['con_image'] = array(
'#type' => 'managed_file',
'#title' => t('Image'),
'#required' => TRUE,
'#default_value' => variable_get('con_image', ''),
'#progress_indicator' => 'bar',
'#progress_message' => 'Uploading ...',
'#upload_location' => 'public://gallery/',
'#theme' => 'test',
'#upload_validators' => array(
'file_validate_is_image' => array(),
'file_validate_extensions' => array('jpg jpeg'),
'file_validate_image_resolution' => array('6000x4000','800x600'),
'file_validate_size' => array(6 * 1024 * 1024),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add to site'),
);
return $form;
}
I call testform after checking additional conditions (like userpoints) via code
$arr = drupal_get_form('testform');
print drupal_render($arr);
The form itself is working, I'm able to add image to node (programmatically) but cannot get the preview of image during upload process. I try to use #theme but it seems doesn't work at all.
My theme function looks like this:
function theme_test($variables) {
$element = $variables['element'];
$output = '';
$base = drupal_render_children($element); // renders element as usual
if($element['fid']['#value'] != 0) {
// if image is uploaded show its thumbnail to the output HTML
$output .= '<div class="multifield-thumbnail">';
$output .= theme('image_style', array('style_name' => 'thumbnail', 'path' => file_load($element['fid']['#value'])->uri, 'getsize' => FALSE));
$output .= '</div>';
}
Any ideas?
You need to declare your theme with a hook_theme in your module.
function yourmodule_theme() {
return array(
'test' => array(
'render element' => 'element',
)
);
}

Drupal7 managed_file form

I have this managed_file form in a Drupal customized module, with this form a user can upload an image and saved it under sites/default/files.
$form['Background_image'] = array(
'#type' => 'managed_file',
'#title' => t('Image'),
'#progress_message' => t('Please wait...'),
'#progress_indicator' => 'bar',
'#description' => t('Click "Browse..." to select an image to upload.'),
'#required' => TRUE,
///'#upload_validators' => array('file_validate_extensions' => array('jpeg jpg png gif')),
'#upload_location' => 'public://backgroundimage/'
'#default_value' => $this->options['Background image'],
);
how to add a function to get the uploaded file?
I tried this but it didnt work.
$image = file_load($form_state['values']['Background_image']);
You should be able to just create the form element, and then use the $form_state['values'] array to get the fid. like this:
function my_module_form() {
$form = array();
$form['background_image'] = array(
'#type' => 'managed_file',
'#title' => t('Image'),
'#progress_message' => t('Please wait...'),
'#progress_indicator' => 'bar',
'#description' => t('Click "Browse..." to select an image to upload.'),
'#required' => TRUE,
'#upload_location' => 'public://backgroundimage/',
'#default_value' => $this->options['background image'] //fid
);
return $form;
}
function my_module_form_submit($form, &$form_state) {
$file = file_load($form_state['values']['background_image']);
$file->status = FILE_STATUS_PERMANENT;
file_usage_add($file, 'module_name', 'entity_name', $entity_id);
file_save($file);
}
I just wrote that on the fly so I'm sure there are syntax errors :) but that's the idea. If you are not getting the file id from $form_state['values']['background_image'], I would try dying in your submit handler and dumping the contents of $form_state['values']:
function my_module_form_submit($form, &$form_state) {
die(var_dump($form_state['values']['background_image']));
}
That should tell you a few things about whats being returned from your form.

drupal 7 presist data in form_state with managed_file

I use D7 managed File.
If i have form error than the form lost file info, i know that is there an error a have to reupload the files.
But in the form validator i have the file save in the db so i have the FId of the file (from file_load)
If I can presist value in form state i can load the file from db in form submit and make it presistent.
in form:
$form['fileUpload'] = array(
'#id' => 'fileUploadId',
//'#type' => 'file',
'#title' => t('upload a file: '),
'#size' => 22,
'#type' => 'managed_file',
'#description' => t('upload file: docx doc pdf'),
'#upload_location' => 'public://',
'#upload_validators' => array(
'file_validate_extensions' => array('docx doc pdf'),
// Pass the maximum file size in bytes
'file_validate_size' => array(4*1024*1024),
),
);
i tried:
I pass the &$form_state by reference in form validator , submit, and form
$form_state['values']['FileInfo'] = $form_state['values']['fileUpload'];
If there is form error form_state lost this value
I pass the &$form_state by reference in form validator , submit, and form
$file = file_load($form_state['values']['fileUpload']);
$form_state['values']['FileInfo'] = $form->fid;
form_set_value($element, $value, &$form_state) for persist form_state data
But it's bit tricky to use.
I found the answer.
i create a hidden field in the form :
$form['infoFile'] = array('#type' => 'hidden', '#value' => '');
in the form validator:
$file = file_load($form_state['values']['candidateCvUpload']);
$form['infoFile']['#parents'] = array('infoFile');
form_set_value($form['infoFile'], $file->fid, $form_state);
submit
$file->status = FILE_STATUS_PERMANENT;
$file = file_save($file);

sending form values $form_state['redirect'] Form API Drupal

I want to send my form values using form API drupal. I have following value
$form['billing']["cardholders_name"] = array(
'#type' => 'textfield',
'#title' => t("Cardholder's Name"),
'#required' => TRUE,
'#prefix' => '<div class="field-wrapper-w1 card-name">'
);
I am writing following code in my form submit function
function test_form_submit($form, &$form_state) {
$form_state['redirect'] = 'www.test.com/page' . '?cname=' . $form_state['values']['billing[cardholders_name]'];
}
But it seems like it is not working. Please help
The following example appears in
http://api.drupal.org/api/drupal/includes%21form.inc/function/drupal_redirect_form/7
$form_state['redirect'] = array(
'node/123',
array(
'query' => array(
'foo' => 'bar',
),
'fragment' => 'baz',
),
);
Any value for form element will be in $form_state under values so in your function test_form_submit you can access cardholders_name
$form_state['values']['cardholders_name']
also you can do it in this way using drupal_goto()
drupal_goto('www.test.com/page' . '?cname=' . $form_state['values']['cardholders_name]);