I would like to modify text that is being converted to a link within CKEditor, using the Linkit plugin. Eg. When linking to a Media Document, append the file type and size.

I am using hook_form_editor_link_dialog_alter in order to modify the dialog form and capture and populate additional attributes on the resulting a element. But there does not appear to be a field for modifying the text.

I tried to set up an event listener in javascript, but am not sure to what or how to attach a listener to CKEDITOR events. Is it possible to listen on a plugin's execution?

Drupal 9.5.9, CKEditor 4, Linkit 6-rc1

  • The 'flow' for this (or at least one that definitely works), is to add something like $form['attributes']['data-foo'] in the hook you mentioned; that in turn gets added to the link, <a data-foo="...">, and then you create a custom filter plugin which looks for that data attribute in the DOM and adds to or modifies the HTML based on the attribute values. If you want to manipulate the HTML in the editor at edit time, I'm pretty sure you need to create a new CKEditor plugin
    – Clive
    Commented May 19, 2023 at 20:32
  • I was hoping to avoid creating an entire plugin, since Linkit was doing 99% of the work and CK4 is deprecated (moving to D10 shortly). I am already adding the attribute, so would like to just 'hook' into the insertion done by Linkit to change the text. Commented May 22, 2023 at 14:57
  • It’s not done by Linkit, it’s done by a plug-in in Drupal core, which Linkit piggy backs off (and which doesn’t raise any JS events as far as I know). Plug-ins in CKE4 we’re difficult to extend, that’s why I’m assuming you’d need to make a new one, not because the functionality doesn’t already exist
    – Clive
    Commented May 22, 2023 at 17:16


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.