Customizing Dynamic Elements
Sylius 2.1 adopts a modern JavaScript architecture based on Symfony UX and StimulusJS. This setup enables you to build dynamic, interactive frontend behavior while following clean Symfony conventions.
This guide explains how to customize and register JavaScript controllers in your Sylius frontend. It covers automatic controller discovery, manual registration, and JSON-based configuration. You’ll also find real-world examples and tips for debugging common issues.
Prerequisites
Sylius 2.1 or later
Webpack Encore is properly configured
Basic knowledge of StimulusJS
Example Use Cases
You might want to customize dynamic elements in cases like:
Adding quantity increment buttons to the product page using a custom Stimulus controller
Creating a sticky "Add to Cart" bar that reacts to scroll events
Updating shipping method options dynamically based on the selected country during checkout
Enhancing admin panel UX with collapsible panels or sortable table rows
Each of these examples relies on connecting a Stimulus controller to a DOM element, configuring it through data-*
attributes, and optionally extending or overriding the default Sylius behavior.
📁 Default Directory Structure
By default, Sylius expects Stimulus controllers to reside in the assets/controllers/
directory. Each controller should be named following the pattern *_controller.js
.
Adding a Stimulus Controller Using Automatic Discovery
This is the simplest way to register a new Stimulus controller. If your controller is located in the expected directory and follows the naming conventions, Symfony UX will automatically detect and load it—no manual configuration needed.
🔧 Steps
1. Create the Stimulus Controller
Place your file under assets/admin/controllers/
and name it using the *_controller.js
pattern.
This controller will automatically be registered as alert
.
2. Create a Twig Template
Use the stimulus_controller()
Twig helper to bind your controller to a DOM element.
Optional: pass data-*
attributes using stimulus_controller()
options if needed.
3. Register the Template via a Twig Hook
Hook your controller’s template into the desired part of the admin UI using the Sylius Twig hook system.
This ensures your controller is rendered at the sylius_admin.order.show.content
hook point, without overriding core templates.
Result
Your controller is discovered automatically.
It's attached to the DOM using
stimulus_controller()
.It’s injected upgrade-safely using a Twig hook.
Disabling or Enabling Existing Stimulus Controllers
In some situations, you may want to disable a built-in controller (e.g., to replace it with your own) or change how it's loaded (e.g., only when needed). This is possible via the controllers.json
file using Symfony UX's controller configuration.
Example: Disable the taxon-tree
Controller in the Admin Panel
taxon-tree
Controller in the Admin PanelTo prevent the built-in taxon-tree
Stimulus controller from loading:
📁 File: assets/admin/controllers.json
This disables the controller entirely (enabled: false
), and even if enabled, it would only load lazily.
🗂 Common Keys
enabled
:true
orfalse
Controls whether the controller is loaded at all.
fetch
:"lazy"
(default) or"eager"
Defines when the controller is fetched:
lazy: loaded only when used in the DOM
eager: loaded immediately on page load
💡 Pro Tip
You can override or replace a disabled controller by registering your own under the same name, or by targeting the same HTML with a new controller.
3: Manual Registration
Use manual registration when:
Your controller is located outside the default
controllers/
directoryYou're integrating a controller from a third-party or custom plugin
You need more explicit control over how and when the controller is loaded
Steps
1. Create Your Stimulus Controller
This controller defines a connect()
lifecycle method and an onClick()
action for buttons.
2. Register the Controller Manually
This ensures that Stimulus knows about your confirm
controller and connects it when used in templates.
3. Create the Twig Template
This adds a button that triggers your onClick
action when clicked.
4. Register the Template via Twig Hooks
Use the Sylius Twig hooks system to inject your custom button into the Thank You page (order confirmation).
This hook ensures that your controller-powered button is shown after order placement, regardless of user type.
⚠️ Compatibility Note
🐞 Troubleshooting Tips
Controller Not Executing? Ensure Webpack Encore has rebuilt your assets (
yarn dev
oryarn build
).Incorrect Controller Name? Verify that the
data-controller
attribute matches the registered controller name.Console Errors? Use browser developer tools to check if your controller is compiled and loaded correctly.
Caching Issues? Clear both Symfony and browser caches to ensure the latest assets are loaded.
Learn More
Last updated
Was this helpful?