Installation
composer require bartosz-maciaszek/trix-bundle
Add the bundle to config/bundles.php:
BartoszMaciaszek\TrixBundle\TrixBundle::class => ['all' => true],
Basic Configuration Add Trix to your form (Symfony 5+):
{{ form_widget(form.content, {'attr': {'data-controller': 'trix'}}) }}
Include Trix assets in your base template:
{{ encore_entry_script_tags('app') }} <!-- Ensure Trix JS is included via Encore -->
First Use Case Create a simple text field in a form:
// src/Form/ArticleType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('content', TextType::class, [
'attr' => ['data-controller' => 'trix'],
]);
}
Ensure trix is enabled in config/packages/trix.yaml:
trix:
enabled: true
Form Integration
data-controller="trix" on textareas to auto-initialize Trix.document.querySelectorAll('textarea[data-controller="trix"]').forEach(el => {
new Trix.Editor(el);
});
Asset Management
config/packages/trix.yaml:
trix:
assets:
js: ['trix', 'trix-attachments'] # Custom JS files
css: ['trix', 'custom-trix-styles'] # Custom CSS
// webpack.config.js
Encore
.addEntry('app', './assets/app.js')
.addEntry('trix', 'trix')
.copyFiles({
from: './vendor/bartosz-maciaszek/trix-bundle/public',
to: 'build/public/[path][name].[ext]',
});
Server-Side Processing
symfony/html-sanitizer):
use Symfony\Component\HtmlSanitizer\SanitizerInterface;
public function submit(ArticleForm $form, SanitizerInterface $sanitizer)
{
$data = $form->getData();
$data['content'] = $sanitizer->sanitize($data['content']);
// Save to DB
}
Dynamic Attachments
trix.yaml:
trix:
attachments:
upload_dir: '%kernel.project_dir%/public/uploads'
allowed_mime_types: ['image/jpeg', 'image/png']
TrixAttachmentsController).Asset Loading
encore is configured to copy Trix assets from vendor/bartosz-maciaszek/trix-bundle/public to public/build/./build/trix.*.CSRF Token Conflicts
config/packages/security.yaml:
security:
access_control:
- { path: ^/trix/attachments, roles: PUBLIC_ACCESS }
HTML Sanitization
symfony/html-sanitizer or white-october/pim-core-bundle's HtmlPurifier.data-trix-content):
$sanitizer->allowAttributes(['data-trix-content' => true]);
Dynamic Forms
$(document).on('trix-change', 'textarea[data-controller="trix"]', function() {
// Handle changes
});
Custom Toolbars
config/packages/trix.yaml:
trix:
toolbar:
buttons: ['bold', 'italic', 'strike', 'bullet-list', 'custom-button']
trix-mentions).Laravel Integration
symfony/bundle bridge:
composer require symfony/bundle
trix.yaml to work with Laravel’s asset pipeline.Testing
$this->getContainer()->get('trix.helper')->setEditorEnabled(false);
symfony/panther:
$client = static::createPantherClient();
$crawler = $client->request('POST', '/upload', [
'trix_attachment' => $file,
]);
Performance
{% if is_granted('ROLE_EDITOR') %}
{{ form_widget(form.content, {'attr': {'data-controller': 'trix'}}) }}
{% else %}
{{ form_widget(form.content) }}
{% endif %}
# config/packages/trix.yaml
trix:
assets:
js: ['trix.min']
css: ['trix.min']
How can I help you explore Laravel packages today?