79642816

Date: 2025-05-28 18:40:26
Score: 1.5
Natty:
Report link

CKEditor does not initialize on new CollectionField items in EasyAdmin Symfony β€” how to fix?

I'm using EasyAdmin 4 with Symfony, and I have a CollectionField that includes form fields with CKEditor (via Symfony's FOSCKEditorType or similar).

CKEditor loads fine for existing collection entries, but when I add a new item dynamically via the "Add new item" button, the CKEditor does not initialize on the new textarea.


πŸ” What I tried:


βœ… Answer:

This is a common issue because CKEditor needs to be manually re-initialized on dynamically added fields inside CollectionField.


πŸ”§ Fix: Manually re-initialize CKEditor when new fields are added

You can do this by listening to the ea.collection.item-added event that EasyAdmin dispatches.

Add this JavaScript to your EasyAdmin layout or as a custom Stimulus controller:

javascript

CopyEdit

document.addEventListener('ea.collection.item-added', function (event) { const newFormItem = event.detail.item; const textareas = newFormItem.querySelectorAll('textarea'); textareas.forEach(textarea => { if (textarea.classList.contains('ckeditor') && !textarea.id.startsWith('cke_')) { // Replace with CKEDITOR.replace or ClassicEditor.create depending on your setup CKEDITOR.replace(textarea.id); } }); });


🧠 Notes:


πŸ“¦ Example (CKEditor 5):

javascript

CopyEdit

import ClassicEditor from '@ckeditor/ckeditor5-build-classic'; document.addEventListener('ea.collection.item-added', function (event) { const newFormItem = event.detail.item; const textareas = newFormItem.querySelectorAll('textarea'); textareas.forEach(textarea => { if (!textarea.classList.contains('ck-editor__editable')) { ClassicEditor.create(textarea).catch(error => { console.error(error); }); } }); });

Reasons:
  • RegEx Blacklisted phrase (1.5): how to fix?
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Naimish Chaurasia