Change relate to dropdown on EditView
2011
Ever wanted to display a relate field as a dropdown list on an EditView? Here is how you can do it.
Assume you have a custom module that has a relate field to Accounts. The module will have a field account_name and a field account_id.
In the vardefs.php for your module add the list of options to the account_id field:
- 'account_id' =>
- array (
- 'name' => 'account_id',
- ...
- 'options' => 'account_list',
- ),
Then in the metadata/editviewdefs.php change account_name to account_id and add a type =>’enum’ to it.
- array (
- 'name' => 'account_id',
- ...
- 'type' => 'enum',
- ),
Then lastly add some code at the top of your modules class file (my_class.php) to create and populate the list. You can of course add more filters to the where clause to display a subset of related records.
- global $db, $app_list_strings, $current_user;
- if ($_REQUEST['action'] == 'EditView')
- {
- $query = "SELECT a.id,a.name FROM accounts a WHERE a.deleted=0 ORDER BY a.name";
- $res = $db->query($query);
- while ($row = $db->fetchByAssoc($res))
- $app_list_strings['account_list'][$row['id']] = $row['name'];
- }
Now do a quick repair on your module to remove the cached vardefs and EditView template and you should see a dropdown list where the select box used to be 🙂
For custom modules built in module builder you should edit the vardefs.php and metadata/editviewdefs.php inside custom/modulebuilder/packages/<package>/modules/<module>/ and the class file in custom/modulebuilder/builds/<package>/SugarModules/modules/<module>/<module>.php.
If the module is a built in module you will have to make the vardefs changes in an extended vardefs file in custom/Extension/modules/<module>/Ext/Vardefs/, change the metadata in custom/modules/<module>/metadata/editviewdefs.php and create the list in an after_retrieve logic hook.
Comment