For various reasons I am currently working on a module with a version of jQuery and jQuery UI that is not core. In other words,
scripts[]=js/jquery.js
scripts[]=js/jquery-ui.js
are declared in the .info
file. While this gives me the correct look and feel of my module, the form callbacks I used previously with Drupal.behaviors
are not working anymore.
My first approach was to remove the (function($) {...})(jQuery);
enclosures in the Drupal.behaviors function, to no avail.
My question is then: Is there a way to write these callbacks using standard (non-Drupal) jQuery or do I have to refactor everything 'the drupal way?'
Here is what I have on the .module side:
function ggent_form($form, &$form_state) {
$form['Input type'] = array(
'#type'=>'fieldset',
'#title'=>'Upload your image'
);
$form['Input type']['file'] = array(
'#type' => 'managed_file',
'#title'=> t('Upload image'),
'#description' => t('An optional description'),
'#upload_validators' => array(
'file_validate_extensions' > array('gif png jpg jpeg'),
'file_validate_size'=>array(512*512*512),
),
'#upload_location'=>'public://',
);
$form['Input type']['submit'] = array(
'#type'=>'submit',
'#value' =>t('Submit'),
'#ajax' => array(
'callback'=> 'ggent_update_image_callback',
'wrapper'=>'mywrapper'
),
);
return $form;
}
///*
// * Form callbacks
// */
function ggent_update_image_callback($form,$form_state) {
$picture_settings = array(
'image' => file_create_url($form['Input type']['file']['#file']->uri)
);
drupal_add_js(array('ggent' => $picture_settings),'setting');
return;
Which should send to my .js file to be used as:
if (input_type === 'image') {
img.src = '....../standardbg.png';
Drupal.behaviors.imgupload = {
attach: function( context, settings ) {
if (typeof Drupal.settings.ggent.image!=='undefined' && img.src !== Drupal.settings.ggent.image) { //typeof img.src !== 'undefined' &&
img.src = Drupal.settings.ggent.image;
}
}
};
}
But this doesn't work. The Drupal.behaviors event doesn't fire at all! Any suggestions would be greatly valued. EDIT: Updating my jQuery using the suggested module does nothing.
(function($) {...})(jQuery);
. EDIT: I also would not be using 2 versions of the same script if I could avoid it! But it doesn't seem to work just using the corejquery.js
.(function($) {...})(jQuery);
is in any way a convention specific to Drupal - it's just a plain old anonymous javascript function, used for scoping. Managing Javascript in Drupal 7 goes into this in plenty of detail, you should read and understand those docs before going any further. There's no such thing as 'Drupal jQuery' and 'non-Drupal jQuery' - they are one and the same