I have written a module with form api which can upload image with dynamic file name. I want to upload that file with auto compression. I cannot understand how will I achieve this. How can I implement this in submit callback.
Below my code:
function MYMODULE_registration_form() {
$form['name'] = array(
'#title'=> t('Name'),
'#type' => 'textfield',
'#required' => TRUE,
'#size' => 40,
'#attributes' => array('placeholder' => t('Enter your name'),),
);
$form['personal_info']['upload_pic'] = array(
'#title' => t('Upload Photo'),
'#type' => 'file',
'#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif, bmp'),
);
$form['submit'] = array(
'#value' => t('Submit'),
'#type' => 'submit',
);
$form['#attributes']['enctype'] = 'multipart/form-data';
return $form;
}
function MYMODULE_registration_form_submit($form, $form_state) {
$name = $form_state['values']['name'];
$count = 1;
$original_username = $name;
while (user_load_by_name($name)) {
$name = $original_username . $count++;
}
$validators = array(
'file_validate_extensions' => array('png gif jpg jpeg bmp')
);
$uploadCheck = file_save_upload('upload_pic', $validators);
if($uploadCheck) {
$parts = pathinfo($uploadCheck->filename);
$imagename = str_replace(' ', '_',$original_username.'_'. $count . '.'. $parts['extension']);
$target = "public://attorneys/".$imagename;
file_move($uploadCheck, $target, FILE_EXISTS_REPLACE);
$picture_fid = $uploadCheck->fid;
}
}
After a lot of R&D I have come to this conclusion and it is working fine:
Implement gulp package and install image optimisation plugin.
Take ref from this url : https://www.tutorialspoint.com/gulp/gulp_optimizing_images.htm
Related
my requirement is to upload files to specific folders. How can I achieve this by using form api.
How can I modify below code such that upload_location should be dynamic. Uploaded file should save into the Folder name provided by the user.
#submit element is not calling custom_document_submit function.
$form['folder_name'] = array(
'#type' => 'textfield',
'#title' => t('Folder Name'),
);
$form['document'] = array(
'#type' => 'managed_file',
'#upload_validators' => array('file_validate_extensions' => array('xml')),
'#upload_location' => 'public://',
'#submit' => array('custom_document_submit'),
);
function custom_document_submit($form, &$form_state){
$destination = $form_state['values']['folder_name'];
$validators = array();
$file = file_save_upload('document', $validators, 'public://'.$destination);
}
The #submit property cannot be declare on a managed_file form object...
Instead, you have to add or to modify a submit action on your form (or button).
$form['#submit'][] = 'custom_document_submit';
If you don't want to modify the submit method of your form, you can also simply add a validator (with the #validate property), witch will modify the '#upload_location' property of your document depending on the folder_name value.
Both, #submit and #validate properties have to be added to the form itself.
<?php
define('IMPORT_DIRECTORY_PATH', 'public://import');
$form['folder_name'] = array(
'#type' => 'textfield',
'#title' => t('Folder Name'),
);
form['document'] = array(
'#title' => t('Upload .xml'),
'#type' => 'managed_file',
'#upload_validators' => array(
'file_validate_extensions' => array('xml'),
),
'#process' => array('import_document_element_process'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Upload'),
'#submit' => array('custom_document_submit'),
);
function custom_document_submit($form, &$form_state){
// Validate extensions.
$validators = array(
'file_validate_extensions' => array('xml'),
);
$file = file_save_upload('document', $validators, FALSE, FILE_EXISTS_RENAME);
// If the file passed validation.
if ($file) {
// Rename uploaded file to prevent cache from remembering name file.
$directory = SCHEDULES_IMPORT_DIRECTORY_PATH;
if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
$uri = $directory . '/xml_' . $file->uid . '_' . $file->timestamp . '.xml';
if ($file = file_move($file, $uri)) {
$form_state['values']['document'] = $file;
}
else {
form_set_error('document', t('The file could not be uploaded.'));
}
}
else {
form_set_error('document', t('The directory is not writable.'));
}
}
else {
form_set_error('document', t('The file extension is not correct.'));
}
// dpm($form_state['values']['document']);
// var_dump( $form_state['values']['document']);
}
/**
* Removing the upload button in managed files.
*/
function import_document_element_process($element, &$form_state, $form) {
$element = file_managed_file_process($element, $form_state, $form);
$element['upload_button']['#access'] = FALSE;
return $element;
}
Hei guys, I'm really new to Drupal and am stuck right now by having selected all the data I need from my database and get to display it inside a drop down list.
Unfortunatly I have no clue on how I can update my database with this specific selected field, as I am not having any value....
$options = array();
foreach ($results as $result) {
$options["name"] = array(
$result->name,
);
}
$options2 = array();
foreach ($results_tour as $result2) {
$options2[$result2->sku] = array(
$result2->title,
);
}
$form['tour'] = array(
'#type' => 'select',
'#options' => $options2,
);
$form['tour_guide'] = array(
'#type' => 'select',
'#title' => t('Tour Guide'),
'#options' => $options,
'#description' => 'Please select a guide for this specific tour'
);
$form['save'] = array(
'#type' => 'submit',
'#value' => 'Speichern',
'#submit' => array('plantours_form_submit'),
);
return $form;
}
function plantours_form_submit($form, $form_state) {
db_update('commerce_line_item')
->fields(array(
'status' => $options["name"]
))
->execute();
}
Don't do it manually, by writing directly to database, but use Drupal commerce api (I guess you are using drupal commerce):
https://www.drupal.org/node/1288414
I am learing batch operations api. i have tried to create a batch operation using a form here is my hook_form
function batch_form_get_form($form, &$form_state){
$form = array();
$form['select'] = array(
'#title' => 'Select',
'#type' =>'select',
'#options' => array(
'batch1' => 'Batch 1',
'batch2' => 'Batch 2',
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit'
);
return $form;
}
here is my form submit function
function batch_form_get_form_submit($form,&$form_state){
$function = 'batch_process_form';
$_SESSION['http_request_count'] =0;
$batch = $function;
batch_set($batch);
dsm ($function);
}
here is my batch process function which i used on batch_set function
function batch_process_form(){
for($i=0; $i<10; $i++){
$operations[] = array('batch_operation_result',array($i));
}
$title = "Batch Started";
$init_message = "Initialing Batch";
$progress_message = "Work in Progress";
$batch = array(
'operations' => $operations,
'title' => $title,
'init_message' => $init_message,
'progress_message' => $progress_message,
);
return $batch;
}
when i tried to run the batch i got the following error
Fatal error: Unsupported operand types in /home/dina/static/d7/includes/form.inc on line 4396
on the line no 4396 of form.inc
$batch_set = $init + $batch_definition + $defaults;
i don't no what i am missing
I think you missed one thing.
In your submit function, instead of
$batch = $function;
try
$batch = $function();
Good luck
I'm trying to register a theme function for a simple form in a custom module, but the theme function is not being called. I just get the basic form.
Here's my hook_theme():
function kss_membership_theme($existing, $type, $theme, $path){
$items = array();
$items['kss_membership_payment_form'] = array(
'render element' => 'form',
);
return $items;
}
for this form:
/**
* Returns the form for the second page of the payment process
*/
function kss_membership_payment_form($form, &$form_state) {
$form['description'] = array(
'#type' => 'item',
'#title' => t('We currently accept Paypal payments'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('kss_membership_payment_form_submit'),
);
$form['#theme'] = array('theme_kss_membership_payment_form');
return $form;
}
and here is the theme function:
function theme_kss_membership_payment_form($variables) {
// Isolate the form definition form the $variables array
$form = $variables['form'];
$output = '<h2>' . t('Please enter your information below') . '</h2>';
$output .= '<div id="personal_details">';
$output .= drupal_render($form['description']);
$output .= drupal_render($form['submit']);
// Additional template output goes here....
$output .= '</div>';
$output .= drupal_render_children($form);
return $output;
}
You are very near to solution, Only one problem is there.
theme calling is wrong
$form['#theme'] = array('theme_kss_membership_payment_form');
you are required to call
$form['#theme'] = array('kss_membership_payment_form');
After that You must required to clear the cache from the admin => configuration => Performance => Clear Cache button.
/**
* Returns the form for the second page of the payment process
*/
function kss_membership_payment_form($form, &$form_state) {
$form['description'] = array(
'#type' => 'item',
'#title' => t('We currently accept Paypal payments'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('kss_membership_payment_form_submit'),
);
$form['#theme'] = array('kss_membership_payment_form');
return $form;
}
I'm building a Form in a module using the Form API. I've had a couple of dependent dropdowns that have been working just fine. The code is as follows:
$types = db_query('SELECT * FROM {touchpoints_metric_types}') -> fetchAllKeyed(0, 1);
$types = array('0' => '- Select -') + $types;
$selectedType = isset($form_state['values']['metrictype']) ? $form_state['values']['metrictype'] : 0;
$methods = _get_methods($selectedType);
$selectedMethod = isset($form_state['values']['measurementmethod']) ? $form_state['values']['measurementmethod'] : 0;
$form['metrictype'] = array(
'#type' => 'select',
'#title' => t('Metric Type'),
'#options' => $types,
'#default_value' => $selectedType,
'#ajax' => array(
'event' => 'change',
'wrapper' => 'method-wrapper',
'callback' => 'touchpoints_method_callback'
)
);
$form['measurementmethod'] = array(
'#type' => 'select',
'#title' => t('Measurement Method'),
'#prefix' => '<div id="method-wrapper">',
'#suffix' => '</div>',
'#options' => $methods,
'#default_value' => $selectedMethod,
);
Here are the _get_methods and touchpoints_method_callback functions:
function _get_methods($selected) {
if ($selected) {
$methods = db_query("SELECT * FROM {touchpoints_m_methods} WHERE mt_id=$selected") -> fetchAllKeyed(0, 2);
} else {
$methods = array();
}
$methods = array('0' => "- Select -") + $methods;
return $methods;
}
function touchpoints_method_callback($form, &$form_state) {
return $form['measurementmethod'];
}
This all worked fine until I added a file field to the form. Here is the code I used for that:
$form['metricfile'] = array(
'#type' => 'file',
'#title' => 'Attach a File',
);
Now that the file is added if I change the first dropdown it hangs with the 'Please wait' message next to it without ever loading the contents of the second dropdown. I also get the following error in my JavaScript console:
"Uncaught TypeError: Object function (a,b){return new p.fn.init(a,b,c)} has no method 'handleError'"
What am I doing wrong here?