0

I am using ckeditor with some extras to render a button. This is part of my ckeditor config in the plugins settings

link: {
      // Automatically add target="_blank" and rel="noopener noreferrer" to all external links.
      addTargetToExternalLinks: true,
      decorators: [
        {
          mode: 'manual',
          label: 'Render as Button',
          classes: ['alo-button-red', 'alo-base-button'],
          attributes: {
            role: 'button',
          }
        },
        {
          mode: 'manual',
          label: 'Render as Inverted Button',
          classes: ['alo-button-white', 'alo-base-button'],
          attributes: {
            role: 'button',
          }
        }
      ]
    }

This works as wanted, but the editor itself shows, after saving once, some wrong markup. Here is the link after creation with decorator "render as button".

Image 1

Then after saving the preview looks like this:

Image 2

So you can see it has blue text, because the editor added a link inside my link. When showing the source I get:

Image 3

Resulting in two nested a tags. Luckily in my frontend I still get only one a tag.

Image 4

But when I edit some things in my wysiwyg editor it happens that those a tags get messed up resulting in a break of all the button code.

Does anybody know why this is happening?

7
  • 1
    In the interests of robust content modelling and future-proofing I'd advise against allowing editors to add buttons directly in a rich text editor. There are plenty of alternative approaches that will serve your needs better in the long run. Commented Nov 4 at 13:33
  • We have not used this approach for years now and you just trade them in for other problems. We'll give this approach a shot this time. Creating richtexts following custom buttons and then again richtexts brings problems with styling for spacing between, which is no in the control of the editor and also brings more configuration effort on the developers side. Furthermore the CP gets crowded with elements (especially when taking into account that one button now needs for fields (target, link, button style and link text). Thanks for sharing your concerns, but we stick to the plan ;).
    – Merc
    Commented Nov 5 at 14:10
  • Agree with James, I don't think this is the right approach. This seems like a very fragile way to do things; as you are discovering. You could add a linky-button entry type to CKeditor, or just add a block type for styled links. Both will prevent editors from accidentally messing things up, which I think is a better approach. An editor should not be able to easily break things, it's the developers job to ensure that. Commented Nov 12 at 10:27
  • …and forcing all external links to open in new tabs is a terrible UX. Commented Nov 12 at 10:28
  • @SethWarburton thanks for your input. Could you clarify how I would do that. How would I add a linky-button entry? With the block style I would add a link and then give the option to wrap a block style around the button to make them appear differently? Is that correct?
    – Merc
    Commented Nov 12 at 11:07

1 Answer 1

0

You can define entry types that are available from within CKEditor, per the docs: https://github.com/craftcms/ckeditor?tab=readme-ov-file#longform-content-with-nested-entries

In your case, you could create a 'button' entry type with fields including the link, the link text, and the 'button' style. For the button style, I'd probably use a dropdown field to allow user only specific types, outputing the classes for those directly to your template, e.g:

option.label: 'Primary button' option.value: 'alo-base-button alo-button-red'

I think that would provide a more robust solution, that's easily extended and potentially re-used in other components.

2
  • This sounds like a very good solution indeed. I will check it out.
    – Merc
    Commented Nov 13 at 12:09
  • I tried this but as we use craft cms in a headless way with graphql this might no be ready :(. So far I have not found any solutions, although they might seem on the way
    – Merc
    Commented Nov 13 at 12:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.