Search code examples
phparraysintersectionsugarcrm

SugarCRM how to get all module fields list with details


I am developing custom module which needs to get list of all fields with details (relations, other attributes) of other modules. Problem occurs when I use other developers custom modules. I used

$dictionary

but not always all module fields is there. Then I made hack of

require_once 'modules/ModuleBuilder/views/view.modulefields.php';
$viewmodfields = new ViewModulefields();
$objectName = BeanFactory::getObjectName($module);
VardefManager::loadVardef($module, $objectName, true);
global $dictionary;

$fieldsData = array();
foreach($dictionary[$objectName]['fields'] as $def) {
   if ($viewmodfields->isValidStudioField($def)){
        $mod_field = array();
        $mod_field['name'] = $def['name'];
        $mod_field['label'] = translate($def['vname'], $module);
        }
    }
}

and something more brutal:

$fieldData = array();

$views = array(MB_EDITVIEW, MB_DETAILVIEW, MB_QUICKCREATE);

// Normlize data
foreach($views as $view) {
   // Get module panels and fields by view
   $parser = ParserFactory::getParser($view, $module);
   $panels = $parser->_viewdefs['panels'];
   $fields = $parser->_fielddefs;

    foreach($panels as $panelName => $panelData) {
        foreach($panelData as $panelDataItem) {
            foreach($panelDataItem as $fieldName) {
                // Check if block field name exists in field array
                if( isset($fields[$fieldName]) ) {
                    // Set unique name in array (name can be in editview and detailview)
                    $fieldData[$fields[$fieldName]['name']] = $fields[$fieldName];
                }
            }
        }
    }
}

But I think that there should be something more native. Can you, please, give me advice?


Solution

  • Loop through the global $moduleList array to get all module names and fetch an empty object for that module using BeanFactory::getBean and use getFieldDefinitions to retrieve the field defs for that module:

    $module_list = array_intersect($GLOBALS['moduleList'],array_keys($GLOBALS['beanList']));
    
    foreach($module_list as $module_name) {
            $bean = BeanFactory::getBean($module_name);
            $field_defs[$module_name] = $bean->getFieldDefinitions();
    }