daddl3/symfony-ckeditor-5-webpack
Installation
composer require daddl3/symfony-ckeditor-5-webpack
npm install @ckeditor/ckeditor5-build-classic
Configure Vite
Update vite.config.js to include CKEditor:
import { defineConfig } from 'vite';
import { ckeditor5 } from 'daddl3/symfony-ckeditor-5-webpack';
export default defineConfig({
plugins: [
ckeditor5({
build: 'classic',
config: {
toolbar: ['bold', 'italic', 'bulletedList']
}
})
]
});
Twig Integration
Add the bundle to config/bundles.php:
return [
// ...
Daddl3\SymfonyCkeditor5WebpackBundle\Daddl3SymfonyCkeditor5WebpackBundle::class => ['all' => true],
];
First Usage In a Twig template:
{{ ckeditor5('my_editor', {
'value': 'Initial content',
'toolbar': ['bold', 'italic']
}) }}
ckeditor5 Twig function to render an editor in a form.toolbar, height).<form method="POST">
{{ form_row(article.content) }}
{{ ckeditor5('article_content', {
'toolbar': ['bold', 'italic', 'numberedList', 'blockQuote']
}) }}
</form>
Symfony Form Integration Use the bundle’s form type for seamless integration:
use Daddl3\SymfonyCkeditor5WebpackBundle\Form\Type\Ckeditor5Type;
$builder->add('content', Ckeditor5Type::class, [
'config' => [
'toolbar' => ['bold', 'italic', 'imageUpload']
]
]);
Dynamic Configuration Override editor config per entity or route:
// In a controller
$builder->add('description', Ckeditor5Type::class, [
'config' => $this->getCkeditorConfigForRoute($request->get('_route'))
]);
Asset Management
public/build/ckeditor5/.// vite.config.js
export default defineConfig({
build: {
outDir: 'public/build',
rollupOptions: {
output: {
entryFileNames: 'ckeditor5/[name].js',
chunkFileNames: 'ckeditor5/[name].js',
}
}
}
});
Image Uploads
Use the imageUpload plugin with a custom upload adapter (e.g., Symfony Uploader):
// vite.config.js
ckeditor5({
plugins: ['ImageUpload'],
config: {
image: {
upload: {
types: ['jpeg', 'png'],
url: '/api/ckeditor/upload'
}
}
}
});
Custom Builds Extend the default build by adding plugins:
// vite.config.js
ckeditor5({
build: {
plugins: ['@ckeditor/ckeditor5-basic-styles', '@ckeditor/ckeditor5-heading']
}
});
Localization Load translations via Vite:
// vite.config.js
ckeditor5({
locale: 'fr',
additionalLanguages: ['fr']
});
Asset Paths
public/build/ckeditor5/ exists and Vite outputs files there. Use absolute paths in Twig:
{{ asset('build/ckeditor5/ckeditor.js') }}
Form Data Binding
Ckeditor5Type form field and ensure the data attribute matches the field name:
{{ form_row(article.content) }}
{{ ckeditor5('article_content') }} {# Must match field name #}
Plugin Conflicts
vite.config.js.Console Errors
Check browser console for 404 errors on CKEditor JS/CSS files. Verify Vite’s outDir and asset paths.
Symfony Profiler Use the Symfony profiler to inspect form data and ensure CKEditor’s output matches the expected format (e.g., HTML-encoded content).
Custom Upload Adapters Extend the upload adapter for cloud storage (e.g., AWS S3):
// vite.config.js
ckeditor5({
config: {
image: {
upload: {
adapter: (loader) => {
loader.file.then((file) => {
// Custom upload logic (e.g., fetch API)
});
}
}
}
}
});
Editor Presets
Create reusable presets in config/packages/daddl3_symfony_ckeditor5_webpack.yaml:
daddl3_symfony_ckeditor5_webpack:
presets:
basic: { toolbar: ['bold', 'italic'] }
advanced: { toolbar: ['bold', 'italic', 'imageUpload', 'code'] }
Use in forms:
$builder->add('content', Ckeditor5Type::class, ['preset' => 'advanced']);
Event Listeners
Listen to CKEditor events (e.g., init, change) via JavaScript:
// In your Vite entry point
import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
ClassicEditor.create(document.querySelector('#my-editor'), {
initCallback: editor => {
editor.model.document.on('change:data', () => {
console.log('Content changed:', editor.getData());
});
}
});
How can I help you explore Laravel packages today?