Customizing SugarCRM Advanced Search in 5.x – Part I

The form itself is part of the new MVC ListView code.

The view is called with the following default params when transitioning from Basic to Advanced Search:

action ListView
module Contacts
search_form_only true
search_form_view advanced_search
to_pdf true

The view controller eventually calls ListViewSmarty with the above. If the above is the example situation, the view controller is found at modules/Contacts/views/view.list.php. The actual call is towards the end of the display() method. This method instantiates the 5.x Search class, populates it with a seed SugarBean (Contacts in this case), then calls setup().

$search->setup() is passed an array named $searchdefs. This is provided by a metadata file generally found in the module’s metadata folder, logically named “searchdefs.php”. setup() will eventually call _build_field_defs() which is tasked with the job of parsing the passed metadata.

To get $searchdefs setup for a custom search criteria, one must add an array with some fairly cryptic and totally undocumented metadata. I only did as much as I needed to get the customization working, but most if not all the valid attributes can be parsed out of the code in include/SearchForm/SearchForm2.php. The pertinent method call is _build_field_defs(), and towards the end of the call, you’ll see feelers for attributes named “function” and “params”. Taking advantage of some of the hooks offered here, you can put together a decent custom search widget as needed (I went the ‘function’ route to return ‘html’).

The pertinent array that I added to $searchdefs[‘Contacts’][‘layout’][‘advanced_search’]:

array(
‘name’ => ‘tags’,
‘label’ => ‘LBL_TAGS’,
‘type’ => ‘varchar’,
‘function’ => array(
‘name’ => ‘testAdvSearch’,
‘include’ => ‘modules/Tags/Tag_utils.php’,
‘returns’ => ‘html’,
)
),
This feeds off merging $searchdef metadata with the module’s vardef metadata. The catch is that it iterates through the module’s metadata before merging, so in effect, you must add a full attribute entry to do any kind of customization. Crap design here.

In 5.x, vardefs are generated from custom/Extension/modules/[module]/Vardefs/[file].php. By adding a simple PHP file with the appropriate array structure, it’s fairly easy to partition out customizations. All files are picked up and merged with “final” or “published” concatenations which are build when running a full Extension rebuild via Admin -> Repair. Don’t forget to do this if you’re putting custom strings in for a given module.

Once the field is complete, then it’s time to worry about processing the user’s input.