Adding onChange event to custom module

I have created a module in module builder. I want to create an onChange event for some of my dropdowns (only within the module). The javascript file I placed in the custom/modyulebuilder/packages/[modulename]/modules/js folder. I am a bit confused where/which file to modify. Tried modifyingthe editviewdefs.php in the custom/modulebuilder/packages… directory -no luck. There are several editviewdef metadata files. Do I change them all of is there a specific editviewdef file that needs to be changed. Also when I re-deploy the module will my javascript changes be lost? How to do this in the most upgrade safe way?

The way I’ve done it (and I’m not saying it’s right but it works) is this: if you’ve created the custom module using module builder, you should have a directory from your docroot called custom/modules/your_module_name. Create a directory in there called js.

Put your javascript/jQuery file in there.

Next, in custom/modules/your_module_name/metadata/editviewdefs.php check if you have an array element called includes. If so, add an array as follows:


      'includes' => 
      array (
        0 => 
        array (
          'file' => 'custom/modules/your_module_name/js/your_js_file.js',
        ),

If it already has files, then just add after them and set the array key number accordingly.

If you don’t have an ā€˜includes’ element, add it after the ā€˜widths’ array block.

Hope this makes sense.

thanks wbjtd,

I got it so the javascript file loads on the page. I am using 7.6.6 btw - and the directory structure is slightly different than what you are suggesting. The modules that I created in module builder are created in the main modules directory like so:

root/modules/RE01_modulename1

root/modules/RE01_modulename2

root/modules/RE01_modulename3 …etc

You suggested the custom/modules folder but it only shows modules that have relationships

there is a custom/modulebuilder directory - it has a builds folder and a packages folder

under builds folder…
builds/ModulenamePackage/sugarmodule/modules/RE01_module1]/metadata.editviewdef.php
builds/ModulenamePackage/sugarmodule/modules/RE01_[module2]/metadata.editviewdef.php

under packages folder
packages/[ModulenamePackage]/modules/[modulename1]/metadata.editviewdef.php
packages/[ModulenamePackage]/modules/[modulename2]metadata.editviewdef.php

In any case the javascript file is linked on the page but what I need now is to add the onchange event to the selection list. Not sure where to make the following addition.

ā€˜javascript’ => ā€˜onchange=doPopulate(this.options[this.selectedIndex].text, ā€œfield_nameā€ );’,

Ok figured it out.

Now I noticed that when I redeployed the module it overwrote my changes (which I was kind of expecting). So, what is the best way to to make these changes so that they will be included in my next deploy? Can I make changes to the custom/modulebuilder/packages/[ModulePackageName]/modules/[modulename]/metadata/editviewdefs.php ?

Which is why I suggested putting custom code files into custom/modules/your_module.

All such files are exported with the module from Studio (and conversely imported into that folder), and any file include changes made in the editviewdefs and detailviewdefs are retained even though they can be amended by using Studio.

1 Like