Skip to main content

Conditional Logic Injection in Models, Views & Controllers

gives developers precise control over the behavior of generated components โ€” such as permissions, filtering, workflow rules, or dynamic form manipulation โ€” while still benefiting from full GUI-driven reusability

Joomla! Component Builder (JCB) enables you to inject custom logic conditionally into your compiled extension's MVC architecture โ€” without directly editing the core files. You can define conditions and corresponding PHP logic at the field, view, or component level, and JCB will integrate the code during compilation exactly where it's needed.

This feature gives developers precise control over the behavior of generated components โ€” such as permissions, filtering, workflow rules, or dynamic form manipulation โ€” while still benefiting from full GUI-driven reusability.

---

๐Ÿง  Where You Can Inject

  • ๐Ÿงฉ Models โ€“ Add logic to queries, prepare data, apply filters
  • ๐Ÿ‘๏ธ Views โ€“ Control layout decisions, prepare view variables, set document properties
  • ๐ŸŽฎ Controllers โ€“ Override save logic, access control, redirects, input sanitation

JCB provides pre-defined insertion points (โ€œcode areasโ€) where logic can be injected via the Custom Code system โ€” and those areas vary depending on the view or controller type.

---

๐Ÿ”ง How It Works

  1. Navigate to your view, model, or controller in the JCB GUI.
  2. Find the Custom Code section or designated code area.
  3. Choose the context (e.g., beforeSave, afterGetItem, display, allowDelete).
  4. Define your logic directly in the GUI.
  5. (Optional) Wrap your logic in a condition, using PHP or JCB placeholders.
  6. JCB will insert that logic at the corresponding point when compiling the component.

You can include placeholders like:


input->get('layout') === 'edit') : ?>
    // Your logic here


Or use dynamic placeholders such as:


if ({user:isSuperUser}) {
    // Special behavior for super users
}

JCB will resolve those at build time to native PHP or Joomla API calls.

---

๐Ÿ”„ Example Use Case

Letโ€™s say you want to prevent saving a record if a field value doesnโ€™t meet a specific rule, but only in Joomla 4:

  • Open the Controller โ†’ beforeSave code area
  • Insert your conditional logic like:

if (JVERSION >= 4 && !$data['my_field']) {
    throw new \Exception('This field is required in J4.');
}

JCB will compile this only in the Joomla 4 version of the component.

Or you want to conditionally load a helper class in a model:


if ($user->authorise('core.manage', 'com_example')) {
    \JLoader::register('ExampleHelper', JPATH_COMPONENT_ADMINISTRATOR . '/helpers/example.php');
}

Again, injected where needed without touching the compiled files.

---

๐Ÿงฐ Related Features

  • ๐Ÿ”ง Custom Code System โ€“ reusable logic blocks with injection points
  • ๐Ÿ”€ Joomla Version Targeting โ€“ resolve conditionals differently per version
  • ๐Ÿง  Field Event Logic โ€“ define JS/PHP logic per field
  • ๐Ÿ’ก Joomla Powers โ€“ use JPKs for clean version-agnostic code
---

โœ… Why It Matters

  • Maintain control over behavior without hacking generated files
  • Implement ACL, filtering, or data integrity logic at the right place
  • Apply version-specific conditions when needed
  • Keep your logic organized, reusable, and tracked