including javascript function in .module file in drupal - 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..

Related

Symfony 2 Nav. Menu, open in new window

I'm attempting to create a new window when a user clicks a link in the navigation menu. What I'm attempting to do at present is just add a target="_blank" item to the url, thereby creating an entirely new page, and from there I'm planning to learn how to change this for my various other needs. The problem is, I'm unable to get the target to go to the associated link.
I've attempted:
$dropdown->addChild('Text', array('route' =>
'routeName', 'routeParameters' => array('parmName' => 'parameter'),
'attr' => array('target' => '_blank'));
But the above results in it not adding the _blank to anything.
$dropdown->addChild('Text', array('route' => 'routeName',
'routeParameters' => array('paramName' => 'parameter')))
->setAttribute('target', '_blank');
Results in the target being set to the li, not to the link itself, as seen below.
<li target="_blank" class="first"> Text
Is there a means to directly set the attribute to the link, so that it will open in a new window when clicked?
Any time and help you can provide is much appreciated.
For what's worth, if you want to do that at the addChild method:
$menu->addChild('Homepage', [
'route' => 'homepage',
'linkAttributes' => ['target' => '_blank'],
]);
If you are using KnpMenuBundle (it sure looks like it)
you can do it like this:
$dropdown->setLinkAttributes(array('target' => '_blank'));

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.

Drupal Hook_Block_View perform action on button click

I am learning drupal and am trying to add some extra features to a module I've made following a tutorial
I have one block 'History' which shows the last x pages you've viewed.
Now I've made a second block with a button 'clear history', but I can't figure out how to make the set_value('trails_block_history','0') happen when my button is clicked (which would clear my history in the database)
anybody who can help me out here?
My blocks:
function trails_block_info() {
$blocks['history'] = array(
'info' => t('History'),
'cache' => DRUPAL_NO_CACHE,
);
$blocks['clearHist'] = array(
'info' => t('Clear history'),
'cache' => DRUPAL_NO_CACHE
);
return $blocks;
}
hook block save:
function trails_block_save($delta = '', $edit = array()) {
variable_set('trails_block_num', $edit['trails_block_num']);
variable_set('trails_block_granularity',$edit['trails_block_granularity']);
}
and the problem:
function trails_block_view($delta = '') {
...
case 'clearHist' :
{
$block['subject'] = 'Clear History';
$block['content'] = '<button>clear history</button>';
} break;
...
Still a student and reaaally new to this (started the module-coding this morning) so sorry if this seems like a stupid question (which it most probably is) but I just can't find it..
Have made another extra feature on the module allready, so I want this one to work as well!
You should use Forms API to create a form with submit button. Then clear your history when form is submitted. More info here and some example code here

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.

Make changes to main form with ahah callback in 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.

Resources