Skip to main content

Custom Scripting Per Field

Attach logic to individual fields โ€” control behavior on save, load, or render

What Is Custom Scripting Per Field?

JCB allows you to assign custom PHP logic to individual fields at key lifecycle moments โ€” such as when a value is loaded (get), saved (store), or rendered (shown in a form or list). This gives you field-level control over how data is processed and displayed, far beyond the standard Joomla field behavior.

You can define reusable scripts or write custom code directly in the fieldโ€™s settings. This logic becomes part of your componentโ€™s compiled output and executes exactly where you need it.

Key Features

  • ๐Ÿง  Save & Get Hooks: Define logic that runs:
    • Before saving the field value (onSave)
    • When retrieving the field value (onGet)
  • ๐Ÿ–ผ๏ธ Render Control: Modify how the field appears in:
    • Admin or Site edit forms
    • List/table views
    • Read-only or config-only displays
  • ๐Ÿ” Multiple Contexts Supported: Apply logic in:
    • Admin edit view
    • Site view
    • Config view
    • Backend list/table view
  • ๐Ÿ’ก Conditional Logic: Add if/else, switch, or decisions based on user group, other field values, or context.
  • ๐Ÿ“ฆ Snippet Integration: Reference reusable Snippets or call shared helper functions.
  • ๐Ÿงช Fully Compile-Time Controlled: Embedded directly into compiled field handlers โ€” no post-compile editing required.
  • ๐Ÿ”’ Safe & Isolated: Field scripting wonโ€™t affect other logic unless explicitly linked.

Common Use Cases

  • Format a date/time value on load
  • Auto-populate a field based on another fieldโ€™s value
  • Run validation or transformation before saving
  • Show/hide fields conditionally at runtime
  • Generate dynamic dropdown values from a helper method
  • Encrypt or sanitize field input before storage

How to Use It

  1. Go to Fields.
  2. Select or create a field.
  3. Find and select the โ€œCustom Fieldโ€ Field Type.
  4. Add PHP code or use getOptions methods. Examples:
    // Get the databse object.
            $db = Factory::getDBO();
            $query = $db->getQuery(true);
            $query->select($db->quoteName(array('a.###ID###','a.###TEXT###'),array('###ID###')));
            $query->from($db->quoteName('###TABLE###', 'a'));
            $query->where($db->quoteName('a.published') . ' = 1');
            $query->order('a.###TEXT### ASC');
    
  5. Save and build the component. The logic will be compiled into the field handler.

๐Ÿง  Pro Tip: Use insert/replace tags if you want the logic to remain editable directly in your IDE post-compile.

Conclusion

Custom scripting per field gives you surgical precision over field behavior โ€” without needing to write full models or controllers. Itโ€™s an ideal way to enhance forms, enforce logic, and fine-tune data handling in a way thatโ€™s clean, reusable, and easy to maintain.