This guide is here to help developers follow best practices when working with Declarative and Serializable Metadata in Sugar. We've identified some common mistakes developers make and put together practical tips on how to avoid them using proven best practices.
Avoid Using $GLOBALS Array
Metadata definitions should directly declare configurations explicitly instead of relying on global variables.
Problematic Example:
$GLOBALS["dictionary"]["MyModule"] = [/* ... */];
Recommended Approach: Use the local $dictionary variable directly:
$dictionary["MyModule"] = [/* ... */];
Eliminate Imperative PHP Code
Avoid embedding business logic or conditional statements directly in metadata files.
Problematic Example:
$label_prefix = 'QUESTIONS_CONFIG'; $mod_strings["LBL_{$label_prefix}_" . strtoupper("view")] = 'My Configuration Panel';
Recommended Approach: Define constants explicitly without dynamic runtime manipulation:
$mod_strings['LBL_QUESTIONS_CONFIG_VIEW'] = 'My Configuration Panel';
Remove Entry Point Validations
Entry point checks are unnecessary in metadata definitions, as these files should be simple data files..
Problematic Example:
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
Recommended Approach: Simply omit such checks from metadata entirely.
Avoid Conditional Class Checks and Dynamic Includes
Don't use conditional logic or dynamic includes in metadata definitions.
Problematic Example:
if (!class_exists('VardefManager')) { require_once 'include/SugarObjects/VardefManager.php'; } VardefManager::createVardef('MyModule','MyModule', ['basic','assignable']);
Recommended Approach: Rely on autoloading and straightforward declarations:
VardefManager::createVardef('MyModule','MyModule', ['basic','assignable']);
Eliminate Imperative PHP Code
Avoid embedding business logic or conditional statements directly in metadata files.
Problematic Example:
$label_prefix = 'QUESTIONS_CONFIG'; $mod_strings["LBL_{$label_prefix}_" . strtoupper("view")] = 'My Configuration Panel';
Recommended Approach: Define constants explicitly without dynamic runtime manipulation:
$mod_strings['LBL_QUESTIONS_CONFIG_VIEW'] = 'My Configuration Panel';
Eliminate Imperative PHP Code
Avoid embedding business logic or conditional statements directly in metadata files.
Problematic Example:
$label_prefix = 'QUESTIONS_CONFIG'; $mod_strings["LBL_{$label_prefix}_" . strtoupper("view")] = 'My Configuration Panel';
Recommended Approach: Define constants explicitly without dynamic runtime manipulation:
$mod_strings['LBL_QUESTIONS_CONFIG_VIEW'] = 'My Configuration Panel';
Eliminate Imperative PHP Code
Avoid embedding business logic or conditional statements directly in metadata files.
Problematic Example:
$label_prefix = 'QUESTIONS_CONFIG'; $mod_strings["LBL_{$label_prefix}_" . strtoupper("view")] = 'My Configuration Panel';
Recommended Approach: Define constants explicitly without dynamic runtime manipulation:
$mod_strings['LBL_QUESTIONS_CONFIG_VIEW'] = 'My Configuration Panel';
Eliminate Imperative PHP Code
Avoid embedding business logic or conditional statements directly in metadata files.
Problematic Example:
$label_prefix = 'QUESTIONS_CONFIG'; $mod_strings["LBL_{$label_prefix}_" . strtoupper("view")] = 'My Configuration Panel';
Recommended Approach: Define constants explicitly without dynamic runtime manipulation:
$mod_strings['LBL_QUESTIONS_CONFIG_VIEW'] = 'My Configuration Panel';
Eliminate Imperative PHP Code
Avoid embedding business logic or conditional statements directly in metadata files.
Problematic Example:
$label_prefix = 'QUESTIONS_CONFIG'; $mod_strings["LBL_{$label_prefix}_" . strtoupper("view")] = 'My Configuration Panel';
Recommended Approach: Define constants explicitly without dynamic runtime manipulation:
$mod_strings['LBL_QUESTIONS_CONFIG_VIEW'] = 'My Configuration Panel';
Eliminate Imperative PHP Code
Avoid embedding business logic or conditional statements directly in metadata files.
Problematic Example:
$label_prefix = 'QUESTIONS_CONFIG'; $mod_strings["LBL_{$label_prefix}_" . strtoupper("view")] = 'My Configuration Panel';
Recommended Approach: Define constants explicitly without dynamic runtime manipulation:
$mod_strings['LBL_QUESTIONS_CONFIG_VIEW'] = 'My Configuration Panel';