file association with large amount of users - drupal

I am sorry if this is a dumb question I have the following code
function trc_upload_file() {
$form['trc_upload_form'] = array(
'#type' => 'managed_file',
'#title' => 'Upload File',
'#descripion' => 'Uplaod files',
);
$form['#submit'][] = 'trc_upload_file_submit';
return $form;
}
function trc_upload_file_submit($form, &$form_state) {
$file = file_load($form_state['values']['trc_upload_form']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
/*file_usage_add($file, 'trc_upload_page', 'user', $account->uid);*/
drupal_set_message(t('You Uploaded Successfully!'));
}
this is working fine
how to use file_usage_add() function, can i use it for user_roles instead individual users.

As Clive has confirmed, this should work:
file_usage_add($file, 'trc_upload_page', 'role', $roleID);
Let us know how you get on,

Related

Drupal 7 Hook_forms not working

Can anyone tell me why this is not working?
The drupal_render(drupal_get_form) is dynamically created in a foreach loop and put into a table theme.
Everything loads except the form fields. I've tried debugging by adding echos and exits to each form function call, but the page continues to load. I am not sure if these functions are simply not being called or if there is some other issue.
foreach( $w as $k => $v ) {
$r[] = array(
'$'.number_format($v->amount, 2),
date('F d, Y', $v->created),
filter_xss($v->paypal_email),
drupal_render(drupal_get_form(('toefl_tutors_admin_withdrawl_request_form_'.$v->id), $v->id))
);
}
function toefl_tutors_admin_withdrawl_request_forms($form_id, $args) {
$forms = array();
if (!empty($args) && $form_id == 'toefl_tutors_admin_withdrawl_request_form_' . $args[0]) {
$forms[$form_id] = array(
'callback' => 'toefl_tutors_admin_withdrawl_request_form',
'callback arguments' => array($args[0]),
);
}
return $forms;
}
function toefl_tutors_admin_withdrawl_request_form($form, &$form_state, $id = 0) {
$form['twid'] = array(
'#type' => 'hidden',
'#value' => $id
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send Money'),
'#attributes' => array('class' => array('btn', 'btn-success'))
);
return $form;
}
I've solved the problem.
I needed to rename the hook_forms function to toefl_tutors_forms() because My module name is actually toefl_tutors not toefl_tutors_admin_withdrawl_request
Apparently and correct me if I am wrong, in order to use hook_forms you must name it mymodulename_forms, not mymodulename_xx_forms.
What confused me was hook_form works perfectly when you name the form function mymodulename_xx_form().

Drupal:Direct a user to a specific page after submitting

I have a submitt drupal form in a my module:
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
And this is my submitt function , what command have to add to direct the user to a specific page with a specific path? I tried this but it didnt work
function testform_submit($form, &$form_state) {
$form_state['submit'] = 'https://www.google.de/';
}
So close...
function testform_submit($form, &$form_state) {
$form_state['redirect'] = 'https://www.google.de/';
}

Drupal 7 retain file upload

I have a file upload form
how can I retain this file when there are other validation errors so that the user doesn't have to upload the file again?
I tried this in my validation function but it doesn't work:
function mymodule_someform_validate($form, &$form_state) {
$form_state["values"]["some_field"] = some_value;
}
the $form_state["values"] variable is not available in my form definition function - mymodule_someform($form, &$form_state)
Any ideas?
Just use the managed_file type, it'll do it for you:
$form['my_file_field'] = array(
'#type' => 'managed_file',
'#title' => 'File',
'#upload_location' => 'public://my-folder/'
);
And then in your submit handler:
// Load the file via file.fid.
$file = file_load($form_state['values']['my_file_field']);
// Change status to permanent.
$file->status = FILE_STATUS_PERMANENT;
// Save.
file_save($file);
If the validation fails and the user leaves the form, the file will be automatically deleted a few hours later (as all files in the file_managed table without FILE_STATUS_PERMANENT are). If the validation doesn't fail, the submit handler will be run and the file will be marked as permanent in the system.
Admin form example for others who may be looking:
function example_admin_form(){
$form = array();
$form['image'] = array(
'#type' => 'managed_file',
'#name' => 'image',
'#title' => t('upload your image here!'),
'#default_value' => variable_get('image', ''),
'#description' => t("Here you can upload an image"),
'#progress_indicator' => 'bar',
'#upload_location' => 'public://my_images/'
);
// Add your submit function to the #submit array
$form['#submit'][] = 'example_admin_form_submit';
return system_settings_form($form);
}
function example_admin_form_submit($form, &$form_state){
// Load the file
$file = file_load($form_state['values']['image']);
// Change status to permanent.
$file->status = FILE_STATUS_PERMANENT;
// Save.
file_save($file);
}

drupal module configuration process uploaded file

How can I process a file upload in a module configuration section? Here is what I have so far.
<?php
function dc_staff_directory_admin_settings()
{
$form['dc_staff_directory_upload_file'] = array(
'#type' => 'file',
'#title' => t('Upload staff directory excel (.xls) file'),
'#description' => t('Uploading a file will replace the current staff directory'),
);
$form['#submit'][] = 'dc_staff_directory_process_uploaded_file';
return system_settings_form($form);
}
function dc_staff_directory_process_uploaded_file($form, &$form_state)
{
//What can I do here to get the file data?
}
If you use the managed_file type instead Drupal will do most of the processing for you, you just need to mark the file for permanent storage in your submit function:
function dc_staff_directory_admin_settings() {
$form['dc_staff_directory_upload_file'] = array(
'#type' => 'managed_file',
'#title' => t('Upload staff directory excel (.xls) file'),
'#description' => t('Uploading a file will replace the current staff directory'),
'#upload_location' => 'public://path/'
);
$form['#submit'][] = 'dc_staff_directory_process_uploaded_file';
$form['#validate'][] = 'dc_staff_directory_validate_uploaded_file';
return system_settings_form($form);
}
function db_staff_directory_validate_uploaded_file($form, &$form_state) {
if (!isset($form_state['values']['dc_staff_directory_upload_file']) || !is_numeric($form_state['values']['dc_staff_directory_upload_file'])) {
form_set_error('dc_staff_directory_upload_file', t('Please select an file to upload.'));
}
}
function dc_staff_directory_process_uploaded_file($form, &$form_state) {
if ($form_state['values']['dc_staff_directory_upload_file'] != 0) {
// The new file's status is set to 0 or temporary and in order to ensure
// that the file is not removed after 6 hours we need to change it's status
// to 1.
$file = file_load($form_state['values']['dc_staff_directory_upload_file']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
}
}
The validate function is probably a good idea as well, obviously you won't need it if the file is not a required field.
This is mostly taken from the image_example module, part of the Examples Module. If you really don't want to use the managed_file type have a look at the file_example module in that same collection, it has examples of how to uploaded an unmanaged file.
Hope that helps

With custom module, display result ,

function searchsong_block($op='list',$delta=0){
$block = array();
switch($op){
case "list":
$block[0][info] = t('Search song');
return $block;
case "view":
$block['subject']='THIS IS SONG SEARCH MODULE';
$block['content']=drupal_get_form('custom1_default_form');
return $block;
}
}
function custom1_default_form () {
$form = array();
$form['txt_name'] =
array('#type' => 'textfield',
'#title' => t('Please enter your name'),
'#default_value' => variable_get('webservice_user_url',''),
'#maxlength' => '40',
'#size' => '20',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Details'),
);
return $form;
}
function custom1_default_form_validate (&$form, &$form_state) {
if(($form_state['values']['txt_name']) == '') {
form_set_error('user_webservice', t('Enter a name'));
}
}
function custom1_default_form_submit ($form_id, $form_values) {
$GET_TXT_VAL = $_POST['txt_name'];
$result = db_query('SELECT title FROM {node} WHERE type = "%s" AND title LIKE "%%%s%%"', 'song', $GET_TXT_VAL);
$output='';
while ($row = db_fetch_object($result)) {
// drupal_set_message($row->title);----IF I ENABLE THIS LINE THEN MY SEARCH RESULT DISPLAYING IN THE GREEN BLOCK, YES I KNOW THIS FUNCTION SOMETHING LIKE ECHO JUST FOR TESTING PURPOSE WE SHOULD USE
$output .=$row->title;
}
$block['content'] = $output;
}
How to print my output ,
Above module does not display anything , even error also,
i thing i should use theme('itemlist') somthing , but i am not sure how to use this , and where i should use this ,
So what i want is , i want to display my search result , in the content region ,
Please find my question picture view below..
Validate and submit are not for outputting data.
You should show your results in custom1_default_form:
Add in submit $_SESSION['search_text'] or use multistep "storage" (learn drupal form api for this).
But let's look how to work via sessions:
Add in custom1_default_form:
// Here is your form code, so form will appear on the top
// ...
if (isset($_SESSION['search_text'])) {
//add here your code from submit that output searching result
$form['result'] = array(
'#type' => 'item',
'#value' => $output,
);
unset($_SESSION['search_text']); // don't forget clear session
}
Your submit function should be like this:
function custom1_default_form_submit($form, &$form_state) {
$_SESSION['search_text'] = $form_state['values']['txt_name'];
// this will store text field into session, then reload page,
// so you drupal_get_form will see entered values.
}
This is all.
Tips:
if(($form_state['values']['txt_name']) == '') {
form_set_error('user_webservice', t('Enter a name'));
}
This code is not needed, if you do:
$form['txt_name'] = array(
... // other properties
'#required' => true,
);
Read please about form api here: http://api.drupal.org
Other way to use block, just call function that output result and form via drupal_get_form, so:
...
$block['content']='custom1_default_result';
...
function custom1_default_result () {
$output .= drupal_get_form('custom1_default_form');
...
$output .= //search result if session filled/
}

Resources