Wrapping drupal-media image in a link produces empty P tags after image

Created on 26 April 2020, about 4 years ago
Updated 7 November 2023, 7 months ago

There's an issue when linking a media item in the editor whereby it adds empty P tags.

It seems to happen when the editor renders, so switching back and forth between source view has the same effect. Or saving, then editing, then saving, then editing, etc.

You get this behavour:

<a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="e2ad1789-3001-430b-a4bb-18760a6ad1c1" href="/node/789">
<drupal-media data-align="center" data-entity-type="media" data-entity-uuid="7d46c2c3-9d96-403b-9519-ace26b847c1b"></drupal-media>
</a>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

Right now I'm just using linkit, but the core link button would do the same thing. Unlinked Media items do not exhibit this behavior. What's the proper answer for someone wanting to use a media item within ckeditor and have it link somewhere specific if this isn't the way? It does work, it just has that undesirable side-effect at the moment.

πŸ› Bug report
Status

Needs work

Version

1.0

Component

ckeditor.module

Created by

πŸ‡¨πŸ‡¦Canada rferguson

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¬πŸ‡§United Kingdom 8bitplateau

    Patch #47 applies to 9.5.3 successfully

  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request β†’ as a guide.

    Want to take a step back and just take a look.

    1. Has this been confirmed in ckeditor5?
    2. If this is strictly a ckeditor4 issue what is the proposed solution, should be added to the issue summary.

  • πŸ‡¬πŸ‡§United Kingdom AaronMcHale Edinburgh, Scotland

    Has this been confirmed in ckeditor5?

    I confirmed in comment #26 that this issue is not relevant in CKEditor5 and so is only relevant to CKEditor4.

    I suspect that measns this issue should be moved to the CKEditor4 contrib module, unless it will actually get fixed in 9.x (which seems unlikely at this point).

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    I doubt it. I’m not sure how ckeditor4 fixes are being handled. If we fix in 9.x it will have to be ported then

  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    Confirmed this issue with CKeditor 4, which I suppose is actually new contrib module.

    I managed to adapt the solution also used in Entity Embed, as found at #3282295: Additional paragraphs added when embedding an entity in CKEditor and linking it β†’ , and suggested by @Graber above in #37.

    The solution was as simple as adding the following lines to the Contrib ckeditor @ modules/contrib/ckeditor/js/plugins/drupalmedialibrary/plugin.js after the existing beforeInit function:

        init: function init(editor) {
          // Prevent adding extra lines.
          editor.dataProcessor.writer.setRules('drupal-media', {
            breakAfterClose: false
          });
        },

    This solution is quite a lot simpler than the patch to date on this issue!

    Attached patch for the ckeditor contrib

  • πŸ‡ΊπŸ‡ΈUnited States lunazoid

    I will say that #53 seems to be working, but I haven't tested it extensively.

  • πŸ‡¬πŸ‡§United Kingdom altcom_nh

    I have tested the patch in #53 with Drupal 9.5.9 and it works, being as it is the same solution as provided for Entity Embeds drupal-entity tags it would seem to be the solution to go for.
    Though we are still using the ckeditor bundled in the core not the contrib so I had to manually apply the changes to the core version js.

    Attached is the version of the patch for the core but using the same fix as #53.

  • πŸ‡ΊπŸ‡ΈUnited States davedg629

    I tested the patch in #53 and it prevents the accumulation of empty p tags, but it is now adding an empty p tag before each media embed that is wrapped in a link/a tag.

    In other words, the code should look like this:

    <p><a href="#">[drupal embed]</a></p>

    But it looks like this:

    <p>&nbsp;</p>
    <a href="#">[drupal embed]</a>
    

    But like I said, this patch is an improvement as additional p tags aren't added every time you save the node like before.

  • πŸ‡ΊπŸ‡ΈUnited States segovia94

    Since Drupal 9 is End of Life today, I am moving this issue to the contrib module and re-rolling the patch from #55.

  • πŸ‡ΊπŸ‡ΈUnited States Shawn DeArmond

    #57 wasn't applying. Refactored it for the current 1.0.x branch.

  • πŸ‡ΊπŸ‡ΈUnited States segovia94

    Added the original test back in.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 5.7
    last update 7 months ago
    50 pass, 2 fail
  • πŸ‡ΊπŸ‡ΈUnited States segovia94

    moved the test to the correct file

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 5.7
    last update 7 months ago
    40 pass, 2 fail
Production build 0.69.0 2024