4

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?

Orbitum
  • 1,585
  • 5
  • 27
  • 47
  • "A custom module which needs to get list of all fields with details". I seriously doubt that your custom module needs to or ought to be getting all of this information. You should get list of visible modules, instantiate empty bean for each one, and get fields from object's fields property. Or don't do whatever you are trying to do, as it's likely overkill – Anthony Mar 02 '15 at 11:20
  • You think that `$bean = BeanFactory::getBean($module); $fields = $bean->field_name_map;` is the best solution? – Orbitum Mar 02 '15 at 11:21
  • Slight modification on answer. You need to check that the module name from moduleList is also in beanList. There are modules in beanList not in moduleList and vice versa, so the module name has to exist in both to trust that it will have the `getFieldDefinitions` method. – Anthony Mar 02 '15 at 12:01

1 Answers1

9

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();
}
Anthony
  • 36,459
  • 25
  • 97
  • 163