Steps to reproduce:

  1. Fresh install with Standard install profile, enable the media_library module
  2. Enable the embedded media filter for a text format and add the Media Library button to the CKEditor configuration.
  3. Use layout builder to create a new custom "Basic block" with an embedded media, without making any changes to the media. The default alignment should be "center."
  4. Edit the newly created inline block, edit the embedded media and change the alignment from "Center" to "Right" but do not change anything else. Save the block.

Expected Results

That the textarea field submits with the changed attribute.

<textarea class="js-text-full text-full form-textarea resize-vertical" data-media-embed-host-entity-langcode="en" data-drupal-selector="edit-settings-block-form-body-0-value" data-editor-value-is-changed="false" data-editor-value-original="<drupal-media data-align=&quot;right&quot; data-entity-type=&quot;media&quot; data-entity-uuid=&quot;3ded04c3-64c6-4125-9144-7073c7ecd2a5&quot; data-view-mode=&quot;&quot;></drupal-media>
" id="edit-settings-block-form-body-0-value--RScU9kHtPt0" name="settings[block_form][body][0][value]" rows="9" cols="60" placeholder="" data-editor-active-text-format="full_html" style="visibility: hidden; display: none;" tc-textcontent="true" data-tc-id="0.5266607481983859">&lt;drupal-media data-align="right" data-entity-type="media" data-entity-uuid="3ded04c3-64c6-4125-9144-7073c7ecd2a5" data-view-mode=""&gt;&lt;/drupal-media&gt;
</textarea>

Actual Results

That the textarea field submits with unchanged data attribute.

<textarea class="js-text-full text-full form-textarea resize-vertical" data-media-embed-host-entity-langcode="en" data-drupal-selector="edit-settings-block-form-body-0-value" data-editor-value-is-changed="false" data-editor-value-original="<drupal-media data-align=&quot;center&quot; data-entity-type=&quot;media&quot; data-entity-uuid=&quot;3ded04c3-64c6-4125-9144-7073c7ecd2a5&quot; data-view-mode=&quot;&quot;></drupal-media>
" id="edit-settings-block-form-body-0-value--RScU9kHtPt0" name="settings[block_form][body][0][value]" rows="9" cols="60" placeholder="" data-editor-active-text-format="full_html" style="visibility: hidden; display: none;" tc-textcontent="true" data-tc-id="0.5266607481983859">&lt;drupal-media data-align="center" data-entity-type="media" data-entity-uuid="3ded04c3-64c6-4125-9144-7073c7ecd2a5" data-view-mode=""&gt;&lt;/drupal-media&gt;
</textarea>

Note the data-editor-value-is-changed attribute on the textarea is still false in this case, and the contents of the textarea have not been updated.

I am trying to figure out if this is a layout builder bug, or a general CKEditor-embedded-in-a-modal bug.

Comments

Dave Reid created an issue. See original summary.

dave reid’s picture

Title: Changing an embedded media's alignment does not get saved » Changing an embedded media's alignment data attribute does not get saved with CKEditor
dave reid’s picture

Issue summary: View changes
dave reid’s picture

I can reproduce this with the steps above. It does not happen when I create a custom block library block, this only happens when being used with layout_builder to create the block.

dave reid’s picture

So the issue seems to be when CKEditor is inside of a ui-dialog (like the off-canvas tray, or if you were to use the Layout Builder Modal module).

dave reid’s picture

Issue summary: View changes
dave reid’s picture

AHA! In drupalmedia/plugin.js:

        data: function data(event) {
          ...
          if (this._previewNeedsServerSideUpdate()) {
            editor.fire('lockSnapshot');
            this._tearDownDynamicEditables();

            this._loadPreview(function (widget) {
              widget._setUpDynamicEditables();
              widget._setUpEditButton();
              editor.fire('unlockSnapshot');
            });
          }

I was curious why we weren't just using 'saveSnapshot' here like we are in other places. I read on https://stackoverflow.com/questions/19878442/ckeditor-4-how-to-make-snap...

While snapshot is locked new snapshots won't be recorder. If snapshot stack was up to date in the moment of locking the snapshot, then unlockSnapshot will update the last snapshot. But if it wasn't then all those changes will not be recorded until next saveSnapshot is fired.

So this isn't committing the change to CKEditor without an additional editor.fire('saveSnapshot') call after the unlockSnapshot call. This resolves the issue with the field not getting saved correctly for me.

dave reid’s picture

Title: Changing an embedded media's alignment data attribute does not get saved with CKEditor » Changing an existing embedded media's alignment or alt data attributes does not get saved with CKEditor
dave reid’s picture

Component: layout_builder.module » media system
Status: Active » Needs review
StatusFileSize
new1.16 KB

With this patch I now see the change event firing for the CKEditor instance when I change existing embed attributes in the dialog and save.

maacl’s picture

Status: Needs review » Reviewed & tested by the community

Thank you for filing the issue and creating a patch! I noticed the same behaviour, and can confirm the patch fixes this.

lauriii’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Thank you for the bug fix! It would be awesome to see test coverage extended for this use case.

dave reid’s picture

dave reid’s picture

Status: Needs review » Needs work

Hrm, looks like there's no FunctionalJavascript test coverage for editing an existing embed at all. :/

dave reid’s picture

Confirmed that this doesn't seem to affect an embedded media in a non-dialog or non-modal CKEditor, like on a regular node form.

dave reid’s picture

This seems to also resolve #3073294: Remove obsolete @todo for "Undo bug when first inserting media into unfocused CKEditor" as with this change I now have the Undo button available after inserting or editing an embedded media in the CKEditor.

mark_fullmer’s picture

I can also corroborate the original problem under the same circumstances and confirm that the addition of editor.fire('saveSnapshot'); per #9 resolves it.

Since there are currently no FunctionalJavascript tests for editing an existing embed, should a separate issue be created to add test coverage, with this issue dependent on its resolution?

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

nod_’s picture

Issue tags: +JavaScript
ravi.shankar’s picture

StatusFileSize
new1.11 KB

Added a reroll of patch #9.

It still need works for tests.

u_tiwari’s picture

Missing a "s" in "saveSnapshot" @ravi

ravi.shankar’s picture

StatusFileSize
new1.11 KB

Thanks @u_tiwari

Updated patch.

narendra.rajwar27’s picture

Status: Needs work » Needs review
phenaproxima’s picture

Priority: Normal » Major
Issue tags: +Media Initiative, +Triaged Media Initiative issue

I can see this being really disruptive for authors (and it's basically a mild case of data loss). Escalating the priority accordingly.

janmejaig’s picture

I have Checked the above ticket for Drupal 9.x.1 found that it is working fine for the "Left & Right" alignment without applying a patch. But it is getting failed for the "center"alignment with or without patch, it is failing in both the cases, please look into the issue .

Steps to add alignment for media library

  1. Go to "Home >Administration >Configuration> Content authoring >Text formats and editors"
  2. Drag and drop "insert from media Library" icon under " Toolbar configuration" section
  3. Note : select or enabled " Embed media " option under Enabled filters
  4. Scroll down & got to "Filter Settings " & click "limit Allowed Html Tags & correct faulty html " link
  5. Add "data-align" under the Allowed HTML tags section & click "save configuration" button
  6. With the above steps, alignment option will be visible without applying the patch.
janmejaig’s picture

Please look this ticket for reference for the center alignment issue as mention below :
#3126783: Image align center not working

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

jastraat’s picture

Status: Needs review » Reviewed & tested by the community

This is still a problem when using ckeditor in a modal (at least in 8.9; I haven't explicitly tested in D9). It affects all attributes of the media item including changing the display/view mode. The patch in 21 fixes the problem. It seems like this is a really nasty bug to be languishing without attention.

phenaproxima’s picture

Status: Reviewed & tested by the community » Needs work

Thanks for reviewing it, @jastraat! I think this issue will need automated test coverage before it can be committed, though (as evidenced by the "Needs tests" tag).

If anyone would like to work on that, there is some documentation on test writing that might offer some guidance: https://www.drupal.org/docs/automated-testing/phpunit-in-drupal. The first two topics on that page probably will prove most helpful in this case.

staceroni’s picture

I have a similar issue using the Media Library embed button within Inline Entity Form. When you embed a media, but make no other changes in the textarea, and update the IEF reference, the changes are not saved. Toggling between the view source and preview of Ckeditor will fix the behavior at that time. This patch happens to fix the broken behavior, although from the conversation here, looks to be a happy coincidence.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

chrisgross’s picture

I can confirm that this is still an issue for Drupal 8.9.15 and that patch #21 seems to work.

paul.linney’s picture

Can also confirm that patch in #21 works with Drupal 9.2.9 with Acquia Site Studio.
Thanks

chrissnyder’s picture

Tested patch #21 with Drupal 9.2.9 successfully. It also solves a related issue I was having where CKEDITOR's change event was not firing right after embedding a media entity.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

yeebot’s picture

I was experiencing the same issue with Drupal 9.2.11 with a Text (formatted, long) field with 2 text formats enabled: Full HTML and Basic HTML. (I'm not sure if relevant but note that Basic HTML format does not allow media entity embedding). The issue occurred when trying to update the alignment of a media entity while in Full HTML mode. I was able to make the change but it would not save.

The patch from #21 resolves the issue.

However I was alternatively able to resolve the issue without the patch by simply removing the Basic HTML format which in my case was redundant, so that only the Full HTML text format applied.

_KurT_’s picture

Can confirm patch from #21 works for drupal 9.3.6

chrisgross’s picture

Status: Needs work » Reviewed & tested by the community

Patch #21 still works on 9.3.6. This really needs to get merged.

phenaproxima’s picture

Status: Reviewed & tested by the community » Needs work

As per #28, this is still needing automated test coverage…

rajab natshah’s picture

Tested 3102249-21.patch with Drupal 9.3.x
Thank you Ravi

jschref’s picture

Tested patch #21 with Drupal 9.3.9 successfully.

johnzzon’s picture

Chiming in to say patch in #21 works perfectly. Adding a new media embed without changing any text now properly updates the CKEditor snapshot.

yogeshmpawar’s picture

Status: Needs work » Needs review
StatusFileSize
new976 bytes
new433 bytes

Updated patch will resolve CS issues.

yogeshmpawar’s picture

Status: Needs review » Needs work

Keeping it in NW as automated test coverage required for this issue.

tim bozeman’s picture

Status: Needs work » Needs review
StatusFileSize
new7.7 KB

Thanks @Dave Reid!

I can manually reproduce this in the browser and the patch fixes it for my project, but for some reason when I create a test for it, it passes in the test environment. 🤔

@Dave Reid, is there some nuance I'm missing with this test?

tim bozeman’s picture

StatusFileSize
new7.65 KB

Whoops, last try to get test-bot to run! 🚀

Status: Needs review » Needs work

The last submitted patch, 45: test-only.patch, failed testing. View results

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

narendra.rajwar27’s picture

Status: Needs work » Needs review
StatusFileSize
new8.63 KB

Adding re-rolled patch for Drupal 9.5.x, combining patch from comment #42 and #45

Status: Needs review » Needs work

The last submitted patch, 48: 3102249-48.patch, failed testing. View results

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kbeck303’s picture

This patch also fixes the display of the view mode not getting updated in the same 'edit media' modal. We are running Core 9.4.8 and used the patch in comment 42

jcnventura’s picture

StatusFileSize
new8.71 KB

Re-roll of #48 for current state of 9.5.x.

dpagini’s picture

I added the test for 9.5.x and it looks like there is one failure here that needs to be looked at, but we are using this patch and it is fixing the problem for us. Seems like this is pretty close otherwise.

jcnventura’s picture

@dpagini Yes, but this now needs to be changed for Drupal10's ckeditor5, and maybe also move the ckeditor changes to the spun-off ckeditor 4 module.

jhedstrom’s picture

Is this still relevant for CKEditor 5? I'm not finding where this code might apply there, so perhaps this issue can be moved to the CKEditor 4 contrib module's queue?

mark_fullmer’s picture

I confirm that I cannot reproduce this problem using CKEditor 5. The original steps to reproduce, in fact, involve a very different process for changing the image alignment (involving a Drupal-provided modal), whereas CKEditor 5's equivalent is done via the native balloon pop-up. I agree that this probably should be moved to the CKEditor 4 contrib module queue.

chrisgross’s picture

Issue tags: -JavaScript +JavaScript

Patch #21 worked for me on 9.3.6, but #52 does not on 9.5.5. The patch applies, but image attributes still do not seem to be saved. Is anyone else experiencing this?

joegraduate’s picture

FWIW, it looks like a ckeditor contrib project issue was already created as a child of this issue: #3330723: Changing an existing embedded media's alignment or alt data attributes does not get saved with CKEditor.

chrisgross’s picture

@joegraduate Yeah, I just realized we switched to using CKEditor 4 in contrib due to the core change, which explains it. The patch in the issue you linked works. Thanks!

andre.bonon’s picture

StatusFileSize
new1.1 KB

Updating the patch to apply to the D9.4.1.

leymannx’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Issue was previously tagged for tests which still appear to be needed.

Also proposed solution should be highlighted in issue summary please

Thanks.

gxleano’s picture

StatusFileSize
new454 bytes

Updating the patch to apply to the D9.3.22.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

shobhit_juyal’s picture

StatusFileSize
new159.65 KB

3102249-port-for-D9.3.22-63.patch

Patch failed for Drupal 10.1.0

socialnicheguru’s picture

Title: Changing an existing embedded media's alignment or alt data attributes does not get saved with CKEditor » Changing an existing embedded media's alignment or alt data attributes does not get saved with CKEditor5
sijumpk’s picture

Tried recreating the issue in Drupal 10 and was unsuccessful. D10 is using CKEditor 5 and this patch is not needed for the alignment to work.