Problem/Motivation
It would be helpful if a javascript event was triggered when a CKEditor5 instance is created. Currently, it is possible for other modules to interact with editor instances using the Drupal.CKEditor5Instances map, but because the editor is created asynchronously, there's not a good way to know when the instance is ready to interact with. By triggering an even during the promise resolution, it would be possible for other code to react to it once it's ready.
Proposed resolution
Let's dispatch an editor:attached event when CKeditors are created.
| Comment | File | Size | Author |
|---|---|---|---|
| #28 | editor-attached-event.patch | 11.37 KB | daaan |
Issue fork drupal-3319358
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
jeffm2001 commentedComment #3
pooja saraah commentedFixed failed commands on #2
Attached interdiff patch
Comment #4
wim leersI'm fine with doing this, but we should not do this in a CKEditor 5-specific way. We should add events to:
Comment #5
nod_+1 to making it not cke5 specific.
We should update the API so that editor attach methods should return a promise, and trigger an event once it's resolved.
Comment #6
wim leersComment #7
jeffm2001 commentedIt makes sense to me to trigger events from Drupal.editorAttach as well, but it wouldn't help for the use case I'm trying to solve. The problem is that
ClassicEditor.create()returns a Promise, which means that Drupal.editorAttach returns before the editor is actually initialized. In order to react to the editor being created, we need an event triggered inside thethenfunction when the Promise is resolved.Comment #8
tim bozeman commentedWhat do y'all think about this patch? It tweaks
Drupal.editors[editor].attachso it expects a promise and then dispatches aneditor:attachedevent.I was doing a really odd work around before finding this issue.
Comment #9
tim bozeman commentedCustom commands...

Comment #10
tim bozeman commented😬
Comment #11
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #12
nod_should be 11.x even
I'd do away with the editorAttachPromise variable and simply call .then() after the .attach().
Also it would be better to use a catch instead of the second parameter of the then method.
Comment #13
tim bozeman commentedWow, that was quick! Thanks for reviewing! Yes indeed, that is more concise. 👍🏻
Comment #14
smustgrave commentedCould we add some test coverage for this new feature?
Also could some of the missing pieces of the issue summary be updated just to help the review
Thanks!.
Comment #17
dinazaur commentedI just opened MR, so it is easier to manage, review, and update the code. It contains same changes as the patch in #13. I just could not apply the patch for the latest D10, so re-rolled it in MR.
Comment #18
tim bozeman commentedIt looks like some extra white space was added when re-rolling #13 into an MR.
Comment #20
s_leu commentedRemoved the whitespaces mentioned in #18 and rebased the branch on 11.x
Comment #21
nesstheheroWas able to spin up an instance using the MR and write a custom module that ties into this event. Seems to work great!
It's wishful thinking, but it'd be nice if this used native web Event Listeners rather than jQuery events, to reduce another need for jQuery in the large scheme of things, but it works as expected.
I won't mark as RTBC since this needs tests, but this is definitely something I could use in my current projects.
Would it be possible to roll this for 10.1? Is there a reason it must wait til 11?
Comment #22
igoragatti commentedRe-rolled patch to work with Drupal 10.1.7
Comment #23
jtwalters commentedThe current patch direction does not account for editors that do not return a Promise. This results in a JS error, like that below:
Uncaught TypeError: Drupal.editors[format.editor].attach(...).then is not a functionExample: Try creating an Ace Editor text format and then switching to it, and then try switching back to a CKEditor 5 type format.
Wrapping with Promise.resolve() should help:
Promise.resolve(Drupal.editors[format.editor].attach(field, format))Comment #24
jtwalters commentedRe-rolled for Drupal 10.2.3 with
Promise.resolve()change per #23Comment #25
heddnCan we get these changes back into the MR? Core doesn't really use a patch workflow any more.
Comment #26
heddnFixed up MR and merged #24 back into it. I've hid all the patches.
Comment #27
smustgrave commentedThanks for turning to an MR.
Moving to NW for the tests and issue summary update.
Comment #28
daaan commentedThe patch #24 does not work with 10.3.1. I have recreated the patch to ensure it is compatible with version 10.3.1.
Comment #29
tim bozeman commentedHere's testing that the
editor:attachedevent is firing.Comment #30
tim bozeman commentedComment #31
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #32
tim bozeman commentedOh dear...
The branch seems pretty good to me, but the bot doesn't like it. I probably should not have rebased against the current 11.x? I'm not sure what to do about that. 😬
Comment #33
tim bozeman commentedComment #36
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #40
oily commentedTried to fix merge conflict in existing MR and rebase 2000+ commits. Created new MR (did not mean to) but since it seems okay, made it the working branch. Hid the other branch. Test-only test fails:
Since we have test coverage, remove 'Needs tests' tag.
Comment #41
oily commentedComment #42
smustgrave commentedTypically really should stick with the existing branch if it was correctly pointed to 11.x and already been through a few reviews.
Comment #43
oily commented@smustgrave Yes, sorry. It was clear I had to handle the conflict locally. I initially set
git config pull.rebase falsefor the git branch but when I tried to fix the conflict using this merge setting I got a huge number of files needing to be fixed. So I changed togit config pull.rebase truewhich means rebase not merge? But then I had problem pushing the branch. Ended up creating a new branch without wanting/ meaning to. Still not sure where I went wrong. Butgit logshowed that the latest 11.x commits instead of one in July 2025 as the HEADSlightly shell-shocked, hoped it would be easier. I have compared the 'changes' between old and new branches. There seem to be 3 changes in each, seem to be the same. I did also pull my local 11.x prior to rebase.