Make changes to main form with ahah callback in Drupal? - drupal

I have a form like poll form.When there is no data in db I want to show only add button and when user clicks "more" I want to show him/her a submit button.
I used the following code but it seems doesn't works.
if ($form['count']['#value'] > 0) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
}
How can I do this?

The values will be in $form_state['values'] so try:
if($form_state['values']['count'] > 0){....
I would expect that at this point the $form['count']['#value'] is not set.

Related

Get out an error message if noone of checkboxes selected

I have set a checkboxes form that works perfect, i have this if statement which also works perfect but what i really want is when the user hasn't selectected any checkbox from the list, when he pushes the save button to get out a drupal error that will says "Oh!you do not select nothing"... How i can do this thing?
if (!$selected) {
drupal_set_message(t('You have to select at least one option from the list.'), 'warning');
}
You can set the message manually from your code:
https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_set_message/7
Where you have that your if logic add drupal_set_message() call.
Do you use Form API?
If yes try the #required property
$form['test'] = array(
'#type' => 'checkboxes',
'#options' => $myOptions,
'#required' => TRUE,
'#title' => t('Test'),
)

Multiple cancel links do not redirect properly in drupal

If a user clicks the "update" link in their recurring fees table, then decides not to make any changes and clicks Cancel instead of Update, they get access denied because the cancel link redirects to the admin view of recurring fees, not back to the user view. This is with the authorize.net handler, the URL in question is like the following:
example.com/user/263/recurring/715/cancel/authorizenet_cim?destination=user/263/recurring-fees
This is the code I got when I am doing research, I changed my code according to the mentioned below,but it does't work for me any help!
In uc_recurring.uc_authorizenet.inc around lines 140-147:
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Update'),
// '#suffix' => l(t('Cancel'), 'admin/store/orders/recurring/view/fee/' . $rfid),
'#suffix' => l(t('Cancel'), $_SERVER['HTTP_REFERER']), //This is the line I have added
);
It is very easy, you can add the Rules, it will redirect.

Drupal 7 - auto submit form after file upload with managed_file type

I got a form with only one field. This field is of the type 'managed_field'. When you click the "Upload" button a progress bar will show you the progress of the file upload. After that you will need to submit the form to save the file.
Since the progress bar won't show up when you select a file and then click the form submit button instead of the "Upload" button. I would like to trigger a form submit after the upload (via the "Upload" button) has completed.
My current form looks like this:
$form['#attributes'] = array('enctype' => "multipart/form-data");
$form['pdf_upload'] = array(
'#title' => t('Upload PDF'),
'#type' => 'managed_file',
'#required' => TRUE,
'#progress_message' => t('Please wait...'),
'#progress_indicator' => 'bar',
'#upload_validators' => array(
'file_validate_extensions' => array('pdf'),
)
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
The file module handles the files via an ajax callback to the file/ajax/* uri. The callback returns ajax commands.
Basically i want to add an extra ajax command that triggers the form submit after the upload of the file has completed.
#Clive That wasn't an option for me since I wanted the users to start the upload themselves. You answer gave me some ideas though and I came up with the following solution.
Drupal.behaviors.fileUpload = {
attach: function(context, settings) {
jQuery("body").ajaxComplete(function(event,request, settings){
// Only do something when on the orders page of a user
// This is where I use the upload functionality
if(window.location.pathname.match(/user\/\d+\/orders/)) {
// Check if the AjaxComplete was triggered by the managed file upload
// pdf_upload_XXX is my form name
// Get the form-build-id from the URL
if (form_build_id = settings.url.match(/file\/ajax\/pdf_upload_\d*\/(.*)$/)) {
// Check if the upload has completed by checking if there is a Delete button in the form that has the form-build-id
if(jQuery('[value="'+form_build_id[1]+'"]').closest('form').find('[id$=remove-button]').length) {
// Click the submit button
jQuery('[value="'+form_build_id[1]+'"]').closest('form').find('[id^=edit-submit]').click();
}
}
}
});
}
}
Hope this is usefull for other users also.
Thnx Clive for setting me on the right path.

how to add a button to drupal forms?

I want to add a custom button to drupal create form so if the user clicked on it instead of submit button, the workflow state of the created object change to another state(not the default first state)
any suggestion?
To modify the default forms generated by drupal you have to add a form_alter hook to your module. You can do it by defining a function like modulename_form_alter assuming the name of your module is modulename. The drupal system passed the form array and the form_state array which you can use to override the default behavior. In your case, the complete function would look something like this.
function modulename_form_alter(&$form, $form_state, $form_id) {
if($form_id == 'what you want') {
$form['buttons']['another_button'] = array(
'#type' => 'submit',
'#value' => 'Add to another state',
'#submit' => array('modulename_custom_form_submit')
);
}
}
function modulename_custom_form_submit($form, &$form_state) {
if($form_state['values']['another_button'] == 'Add to another state') {
//Do your thing
}
}
After you made the necessary modifications, you can simply submit to the default submit action of the creation form.

including javascript function in .module file in drupal

on click of button i want hi message to be displayed using javascript in drupal.I have made a .js file and know that to incude that i must use drupal_add_js(drupal_get_path('module', 'document') .'/click.js'); but the problem is to create button i used $form['click'] = array(
'#type' => 'button',
'#attributes' => array('onclick' =>drupal_add_js(drupal_get_path('module', 'document') . '/cancel.js')),
'#value' => t('click'),
);
I want that hi message which i have included in js file to be shown when button is clicked.
Please help
Hi thanx for your concern..........
here is the way i proceeded in .module file
function document_form(&$node) {
$form['click'] = array(
'#type' => 'button',
'#attributes' => array('onclick' =>message()),
'#value' => t('click'),
);
}
function document_form_alter(&$form, &$form_state, $form_id) {
drupal_add_js(drupal_get_path('module', 'document').'/cancel.js', 'module');
$settings['click'] = array(
'nid' => $form['nid']['#value'],
'cid' => $form['cid']['#value'],
'uid' => $form['uid']['#value'],
'pid' => $form['pid']['#value'],
);
drupal_add_js($settings, 'setting');
}
and my .js file code is as follows:
function message()
{
alert("This alert box was called");
}
<body>
</body>
but still onclick of button i m not getting the message "This alert box was called"
Kindly help where the problem is coming now.......
Thanx in advance....
in wait of your response
The form alter won't add the JS file in the way you are wanting.
In the function you create the form you can use drupal_add_js, outside of the creation of the form array.
Then you can use the onclick to call the function in your JS file.
A better way to do this is to use drupal behaviours to add an click listner to the button (see example here).
Looks like simplest solution would be, as you actually don't need the button to go to submit and other form stuff.
add a link to the text
style the the link as button using css .mybuttons{}
hook the js on the id. $(#mybutton1).alert..

Resources