7.4 Admin: Template Migration
This step converts templates from Semantic UI to Bootstrap 5 and integrates them with Sylius 2.0 Twig Hooks system.
When to skip this step:
Your plugin has no admin templates
Templates are already using Bootstrap 5 and Twig Hooks
When to do this step:
You have admin forms (all resources need Twig Hooks in Sylius 2.0)
You want to make templates customizable in end application
Important: Execute this migration for ALL resources with admin forms, not just resources with custom templates. In Sylius 2.0, all admin forms require Twig Hooks configuration.
📊 Overview & Recommendations
Two Independent Decisions:
####Decision 1: Template Structure
Static Templates - All fields in one file, not customizable by end application
Twig Hooks with sub-hooks - Each field in separate template, full flexibility
Decision 2: Form Type
Normal Forms - Traditional forms without interactivity
Live Components - Server-side interactivity (live validation, dynamic actions)
🎯 Recommended Approach
For 90% of cases: Twig Hooks + Basic Live Component
✅ Why this is recommended:
End application can customize every field
Live validation out of the box (no extra work!)
Uses Sylius's built-in
ResourceFormComponent- no custom code neededMaximum flexibility with minimal effort
When to add Custom Live Component:
You need custom actions (e.g., auto-generate slug from name)
You have field dependencies
You need custom form behavior
When to skip Live Components:
Very simple forms where live validation isn't needed
Performance-critical scenarios (rare)
🎯 Decision Guide
✅ RECOMMENDED: Twig Hooks + Basic Live Component
This is the recommended approach for most plugins. Follow these steps:
1. Add hook prefix to routing
Already done in Routing step:
This creates hook: plugin.admin.resource.{action}.content.form.sections
2. Import Twig Hooks config
3. Configure Live Component service
First, configure the service so you know what to reference in hooks.
config/services/twig_component.xml:
Key value: The key attribute (plugin:admin:resource:form) is what you'll reference in hook configuration.
4. Create hook configuration
Create config/twig_hooks/admin/resource/create.yaml:
Key points:
Component uses
@SyliusAdmin/shared/crud/common/content/form.html.twig- Sylius's default form wrapperOnly create custom
form.html.twigif you need custom form theme or modificationsdefault: enabled: false- prevents double rendering when using sub-hooks
5. Create section templates
Main section (templates/admin/resource/form/sections/general.html.twig):
Field template (templates/admin/resource/form/sections/general/code.html.twig):
6. (Optional) Handle translations with Sylius helper
Only if your resource has translations and you want accordion UI:
Add to hook configuration:
Create translations section (templates/admin/resource/form/sections/translations.html.twig):
This helper automatically:
Creates accordion for translations
Allows sub-hooks for each translation field
Handles locale switching
Add sub-hooks for translation fields:
Create translation field templates (templates/admin/resource/form/sections/translations/name.html.twig):
7. Copy for update action
Copy create.yaml to update.yaml and change create to update in hook names.
8. Validate
Check that your hooks appear in the list.
That's it! Forms are now migrated to Bootstrap 5 with Twig Hooks and Live Components.
🔧 Advanced: Custom Form Template
Only create custom form.html.twig if you need:
Custom form theme
Custom form attributes
Additional wrapper markup
Example (like in this plugin for custom form theme):
templates/admin/resource/form.html.twig:
Then update hook configuration to use your custom template:
🔧 Advanced: Custom Live Component Actions
If you need custom actions (e.g., auto-generate slug from name), follow these additional steps:
Before You Start: Check if you have existing JavaScript assets that already implement this logic. If you do, consider whether the JavaScript should be:
Rewritten as Live Component actions (this section)
Migrated to Stimulus Controller (see Assets Migration)
Left as-is and migrated to assets/admin/
See the Assets Migration guide for analyzing existing JavaScript before adding custom Live Component actions.
1. Create custom PHP Component
src/Twig/Component/Resource/FormComponent.php:
2. Update service definition
Change the service class to your custom component:
3. Use the action in templates
You can now call your custom action from templates with data attributes.
📝 Alternative: Twig Hooks without Live Components
If you don't want Live Components (not recommended), don't add the component configuration. Your hooks will work with normal forms.
Hook configuration without component:
What you lose:
No live validation
No live form state
Form submits like traditional forms
What you keep:
Fully customizable via Twig Hooks
Bootstrap 5 styling
End applications can override any field
📝 Alternative: Static Templates (Not Recommended)
If you want the simplest approach with no customization, create one template with all form fields hardcoded. You still need to use Twig Hooks, but only a single hook pointing to one template file.
Single template example (templates/admin/resource/form/sections/all_fields.html.twig):
Hook configuration (config/twig_hooks/admin/resource/create.yaml):
Copy to update.yaml and change create to update.
Why not recommended:
End application cannot customize individual fields
No flexibility for end users
Hard to maintain for complex forms
🔍 Finding Existing Sylius Hooks
For developers:
Open admin page in browser
Open Symfony Profiler → Twig Hooks tab
See all available hooks with their structure
🎨 Bootstrap 5 Migration
Sylius 2.0 uses Bootstrap 5 instead of Semantic UI. Use standard Bootstrap classes in your templates.
Common patterns:
Container:
container,container-fluidGrid:
row,col-*Cards:
card,card-header,card-bodyButtons:
btn btn-primary
Last updated
Was this helpful?
