Ran in to an issue where I was getting a JavaScript error stating the CKEditor instance already exists. This patch fixes the issue by destroying the instance if it already exists before trying to replace it.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mkesicki’s picture

Thank you for your patch. We will check this as soon as possible.
Please be patient.

dczepierga’s picture

Status: Needs review » Postponed (maintainer needs more info)

@greenSkin, could u write how to reproduce this?

Greetings

greenSkin’s picture

Status: Postponed (maintainer needs more info) » Needs review

The steps to reproduce can be a little tricky as it is AJAX related. Basically you load the page either with the textarea loaded on page load or via an AJAX callback; no problems, everything works fine. The issue comes when you remove that textarea and replace it with the same one. The CKEditor doesn't know it was replaced so when the behaviors tries to initialize the new editor it errors that it already exists.

The simplest of fix is what the patch attempts, to destroy any pre-existing editor before trying to initialize it again.

Hope that helps. :)

dczepierga’s picture

@greenSkin, could u try review my patch? It will be better to destroy CKEditor by textarea_id - if it will be more CKEditor instances in one page u will destroy last one replaced - not selected in that moment...

Greetings

greenSkin’s picture

Status: Needs review » Reviewed & tested by the community

@dczepierga: Yes, that works wonderfully. Thank you!

dczepierga’s picture

Title: Destroy existing instance when trying to replace it » [D6] Destroy existing instance when trying to replace it
Status: Reviewed & tested by the community » Fixed

Changes commited to GIT (diff)

Thx for help and review patch.

Greetings

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

mkesicki’s picture

Title: [D6] Destroy existing instance when trying to replace it » [D6] Destroys existing instance when trying to replace it
alsammanKhaled’s picture

Issue summary: View changes

I am not sure if this is related or not but i am using drupal 9 and i am having a smiliar issue. when i add my ckeditor using js and remove the old one it would give this error "ckeditor.js?v=4.17.1:350 Uncaught TypeError: Cannot read properties of null (reading 'unselectable')"
when inspecting the element the ckeditor is not rendered. however without js it would render everything. I end up with a textfield element that is empty whe i use my js. is this related to here or should i make an issue or search somethinge different? any guidance is appreciated.