Skip to main content

Save/Get Hooks per Field

Add custom logic that runs automatically when a fieldโ€™s data is loaded or saved โ€” without writing external plugins or overriding models

What It Does

JCB allows each field to define PHP "hooks" that are triggered:

  • onSave: when the fieldโ€™s data is being saved
  • onGet: when the fieldโ€™s data is being retrieved for display or processing

These hooks are per-field, defined visually in the GUI, and are injected directly into the generated model or controller logic during compile. This provides pinpoint customization without breaking Joomla MVC conventions.

Types of Hooks

๐Ÿ“ onSave

Run logic right before the field is stored in the database:

  • Sanitize or format input
  • Combine multiple inputs into a single field
  • Generate a slug or token
  • Apply conditional business logic (e.g., if X, then modify Y)

๐Ÿ“ค onGet

Run logic when data is fetched from the database before it's displayed or processed:

  • Reformat values (e.g., date or currency display)
  • Replace tokens or placeholders
  • Inject dynamic data or perform lazy joins
  • Translate stored codes into readable text

How to Configure in JCB

  • Go to Field โ†’ Rules/Logic โ†’ โ€œCustom Save Codeโ€ or โ€œCustom Get Codeโ€
  • Use valid PHP inside the textarea (no PHP tags needed)
  • Reference the current field value as $value
  • Return the transformed value at the end

Example

For a field named alias:

On Save (slugify input):
$value = strtolower(preg_replace('/[^a-z0-9]+/', '-', $value));
return trim($value, '-');
On Get (wrap in badge for display):
return '<span class="uk-label">' . $value . '</span>';

Benefits

  • โš™๏ธ Encapsulated logic per field: Keep save/get behavior tightly scoped to the data it affects.
  • ๐Ÿงฑ Reduces controller/model overrides: No need for post-processing elsewhere โ€” it happens inline.
  • ๐Ÿงช Round-trip compatible: Hooks are stored in the database and regenerate during every compile.
  • ๐Ÿ” Repeatable across components: Field types can carry these hooks by default to simplify reuse.

Conclusion

Save/Get hooks give you field-level logic injection with zero boilerplate. Whether you're transforming data, creating calculated values, or modifying output dynamically, these hooks make your fields smarter โ€” right from the GUI.