Hello !!
I'm developing a "field" module and would like to use the overlay for some processes.
I added a hook_menu to declare a form like this :
function mymodule_menu() {
$items = array();
$items['mymodule/processes/%'] = array(
'title' => 'Processes',
'page callback' => 'drupal_get_form',
'page arguments' => array("mymodule_processes_form", 2),
'access arguments' => array('access overlay'),
'type' => MENU_CALLBACK,
);
return $items;
}
I added a hook_admin_paths_alter too, to declare that my form path is included in overlay display :
function mymodule_admin_paths_alter(&$paths) {
$paths['mymodule/processes/*'] = TRUE;
}
Finally, I have a js file loaded to add an overlay link in my field :
(function ($) {
Drupal.behaviors.mymodule_widget = {
attach: function( context ) {
$('.field-widget-mymodule-mymodule', context).each( function() {
var fullid = $(this).attr("id");
var id = fullid.replace("edit-", "");
var drupalid = id.replace("-", "_");
var btn = '<a href="/mymodule/processes/"+drupalid+'">process new field</a>';
$("#" + fullid + " fieldset").append(btn);
} );
}
}
})(jQuery);
When I click on my link, it open the child form in the overlay like I want.
Now in my form function declaration, I would like to attach some javascript files with drupal_add_js :
function mymodule_processes_form( $form, $form_state, $fieldname ) {
drupal_add_js( drupal_get_path( "module", "mymodule" ) . "/js/mymodule.js" );
drupal_add_css( drupal_get_path( "module", "mymodule" ) . "/js_lib/js_lib.css" );
drupal_add_js( drupal_get_path( "module", "mymodule" ) . "/js_lib/swfobject.js" );
drupal_add_js( drupal_get_path( "module", "mymodule" ) . "/js_lib/jquery.js_lib.js" );
// code to display form elements...
}
My "mymodule.js" file contains following code :
(function ($) {
Drupal.behaviors.mymodule = function( context ) {
alert( "test" );
}
})(jQuery);
But when I click on the link to display my form in overlay, there is no alert displayed. With firebug, I can see that the "mymodule.js" file is correctly loaded, but it seems that it doesn't execute the Drupal.behaviors.mymodule function.
I tried to add a hook_overlay_child_initialize function in my module to load the js files used by the child form, but nothing append...
I don't know what is wrong in my module, but I can't have the js execution on child form display. If anyone can help me it would be nice.
Thanks in advance for your help.
Comments
I solve the problem... I
I solve the problem...
I token a javascript file from a drupal6 module and js syntax is not the same for drupal7 :
I must use "attach: function" instead of Drupal.behaviors.mymodule = function()...
http://www.sabugo.ch
http://www.titouille.ch
They are different parts of
They are different parts of the Drupal object.
Drupal 6: Drupal.behaviors.MODULENAME = function()
In the D6 version, you attach a function named MODULENAME to the
Drupal.behaviorsobject. It allows code to be executed inside of the function, and for something to happen. No value needs to be returned.Drupal 7: Drupal.behaviors.MODULENAME = {}
In the D7 version, the you attatch an object named MODULENAME to the
Drupal.behaviorsobject. By attaching an object, it gives more options as to what can be done. You asked about why you had to use "attach", it is because the function that was atDrupal.behaviors.MODULENAME()in D6, is now atDrupal.behaviors.MODULENAME.attach()in D7. This allows for there also to beDrupal.behaviors.MODULENAME.detach()to remove behaviors if necessary. And other properties can be addedDrupal.behaviorsobject besides just "attach" and "detach" if necessary.So the answer is, it makes the Drupal.behaviors object more flexible.
Contact me to contract me for D7 -> D10/11 migrations.
Thanks for your answer Jay. I
Thanks for your answer Jay. I better understand the way to use the js Drupal object now. I'm learning hard D7 theses times and it's not so easy because there is lot of new ways to achieve same goals as D6.
but I don't discourage myself ;-)
http://www.sabugo.ch
http://www.titouille.ch