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.
General Guidelines for Declarative Metadata:
- Static Definitions: Always use static and explicit values rather than computed ones.
- Avoid Direct Database Calls: Metadata should not perform direct database queries
Avoid Using $GLOBALS Array
Metadata definitions should directly declare configurations explicitly instead of relying on global variables.
Problematic Example:
Recommended Approach: Use the local $dictionary variable directly:
Eliminate Imperative PHP Code
Avoid embedding business logic or conditional statements directly in metadata files.
Problematic Example:
Recommended Approach: Define constants explicitly without dynamic runtime manipulation:
Remove Entry Point Validations
Entry point checks are unnecessary in metadata definitions, as these files should be simple data files..
Problematic Example:
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:
Recommended Approach: Rely on autoloading and straightforward declarations:
Note: we plan to introduce a better way of extending vardefs from module templates. It will be more declarative and should avoid VardefManager::createVardef calls inside the vardefs files.
Logic hooks
Separate definition from implementation where Metadata declares where Logic Hook should be triggered from.
Problematic Example:
Recommended Approach: custom/modules/logic_hooks.php
must contain the Logic Hook definition and not the class itself
Metadata files
(view defs, vardefs, etc) that are supposed to have specific code defined as arrays should not have any function calls in them or any other logic like string concatenation or loops.
Problematic Example:
Views/Layouts definitions
Compiled definitions should only contain array definitions.
Problematic Example: existing code in custom/application/Ext/clients/base/layouts/header/header.ext.php
Problematic Example 2: existing code in custom/modules/Contacts/Ext/clients/base/views/record/record.ext.php
Recommended Approach: metadata definition only: