如何在SuiteCRM v7.9中创建带关联筛选的自定义ListView页面
Got it, let's walk through how to build this custom filtered list page for your SuiteCRM 7.9 instance (based on Sugar 6.5 CE). This approach will mirror the standard ListView's look and feel (including checkboxes) but only show records linked to your other custom module. Here's a step-by-step breakdown:
First, we need a controller to handle the request for your custom list page. Create a new file at custom/modules/[YourCustomModule]/controllers/customlinkedlist.php:
<?php require_once('include/MVC/Controller/SugarController.php'); class [YourCustomModule]ControllerCustomlinkedlist extends SugarController { public function action_list() { // Set the custom view we'll create next $this->view = 'customlinkedlist'; // Check ACL permissions to ensure the user can access this module if(!$this->bean->ACLAccess('list')) { sugar_die($GLOBALS['app_strings']['LBL_NO_ACCESS']); } } }
Replace [YourCustomModule] with the actual name of your module (e.g., MyCustomModule).
Next, create a custom view that extends the standard ListView, so we can reuse most of its functionality but add our custom filter. Create custom/modules/[YourCustomModule]/views/view.customlinkedlist.php:
<?php require_once('include/MVC/View/views/view.list.php'); class [YourCustomModule]ViewCustomlinkedlist extends ViewList { public function preDisplay() { parent::preDisplay(); // Set a distinct page title to differentiate from the regular list $this->lv->listTitle = 'Linked ' . $GLOBALS['mod_strings']['LBL_MODULE_NAME']; } protected function get_list_query($params = array()) { // Grab the base query from the parent ListView $query = parent::get_list_query($params); // Add custom join/filter to only include records linked to your other custom module // Replace [RelatedModule] with your associated module's name // Replace [link_name] with the relationship name between your two modules (find this in Studio > Relationships) $link = $this->bean->get_link_relationship('[link_name]'); $related_table = $link->getJoinTable(); $related_module_id_col = $link->getRelatedModuleIdColumn(); // Use INNER JOIN to only keep records with existing association rows $query .= " INNER JOIN $related_table ON $related_table.id = {$this->bean->table_name}.id"; $query .= " WHERE $related_table.$related_module_id_col IS NOT NULL"; return $query; } }
This overrides the default list query to filter out any records that don't have an active link to your target custom module.
To keep the checkbox functionality and UI matching the regular ListView, we don't need to build a new template from scratch. Since our view extends ViewList, it will automatically use your module's existing listviewdefs.php and list.tpl files.
If you want small UI tweaks (like a note explaining the filtered list), copy the default tpl to custom/modules/[YourCustomModule]/tpls/view.customlinkedlist.tpl and modify it:
<div class="alert alert-info"> * This list only shows records linked to [RelatedModule] </div> {include file="modules/[YourCustomModule]/tpls/list.tpl"}
To make this custom page easy to find, add a menu item to your module's navigation. Create or edit custom/modules/[YourCustomModule]/metadata/menudefs.php:
<?php $module_menu[] = Array( "index.php?module=[YourCustomModule]&action=customlinkedlist", "Linked Records List", "Linked[YourCustomModule]", "[YourCustomModule]" );
This adds a new option under your module's menu labeled "Linked Records List".
After setting up all files, head to Admin > Repair > Quick Repair and Rebuild to clear SuiteCRM's cache. Then access your custom page via the new menu item or direct URL: index.php?module=[YourCustomModule]&action=customlinkedlist.
You should see:
- A filtered list of records only linked to your target custom module
- Checkboxes next to each record (identical to the standard ListView)
- Working bulk actions (if enabled for your module)
- Double-check relationship names and table columns via Studio or
custom/modules/[YourCustomModule]/metadata/relationships.phpif the filter doesn't work as expected. - Test with a non-admin user to confirm ACL permissions are enforced correctly.
- If you need to adjust the query further (e.g., add additional filters), modify the
get_list_querymethod in the custom view.
内容的提问来源于stack exchange,提问作者Jeffiekins

