- Issue created by @nyanmar
- Status changed to Postponed: needs info
about 1 year ago 1:37pm 12 February 2024 - 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺
According to the
InsertDrupalMediaCommand
docs this should be possible:/** * The insert media command. * * The command is registered by the `DrupalMediaEditing` plugin as * `insertDrupalMedia`. * * In order to insert media at the current selection position, execute the * command and pass the attributes desired in the drupal-media element: * * @example * editor.execute('insertDrupalMedia', { * 'alt': 'Alt text', * 'data-align': 'left', * 'data-caption': 'Caption text', * 'data-entity-type': 'media', * 'data-entity-uuid': 'media-entity-uuid', * 'data-view-mode': 'default', * }); * * @private */ export default class InsertDrupalMediaCommand extends Command {
I wonder if this is a bug in
DrupalMediaUI
, where it does:this.listenTo(buttonView, 'execute', () => { openDialog( libraryURL, ({ attributes }) => { editor.execute('insertDrupalMedia', attributes); }, dialogSettings, ); });
👆 as you can see, it passes on all attributes it receives!
- Status changed to Fixed
about 1 year ago 3:43pm 29 February 2024 - 🇦🇺Australia mstrelan
I was able to get the
data-caption
attribute inserted in the source by adding it in thethis.attrs
andthis.converterAttributes
in the constructor of theDrupalMediaEditing
plugin thanks to @rikki_iki but it isn't editable, and pressing the "Toggle caption on" button adds an extra caption, overriding the original.Attaching a patch to demonstrate, and a screencast below.
- Status changed to Active
about 1 year ago 1:13am 14 March 2024 - 🇦🇺Australia mstrelan
I've used CKEditor Inspector to compare the model of a working media with caption with what I'm getting with the patch.
Working:
<drupalMedia drupalMediaEntityType="media" drupalMediaEntityUuid="..." drupalMediaIsImage="true" drupalMediaType="image"> <caption>Test caption</caption> </drupalMedia>
Not working:
<drupalMedia drupalMediaCaption="Test caption" drupalMediaEntityType="media" drupalMediaEntityUuid="..." drupalMediaIsImage="true" drupalMediaType="image"> </drupalMedia>
I believe the issue is that the upcast converter is doing a simple
attributeToAttribute
conversion whereas it needs special handling for the caption. I guess theDrupalMediaCaptionEditing
should take care of that but it just needs to be triggered some how. - Status changed to Needs review
about 1 year ago 3:32am 14 March 2024 - 🇨🇦Canada jibran Toronto, Canada
Updated the title and re-categories the issue. Let's update the IS with steps to reproduce.
- Status changed to Needs work
about 1 year ago 3:44am 14 March 2024 The Needs Review Queue Bot → tested this issue.
While you are making the above changes, we recommend that you convert this patch to a merge request → . Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)
We've just run in to the same problem after upgrading D9 to D10. We also use a custom media library opener to set a default caption on images inserted into CKEditor via the media library. It worked in D9 but doesn't work in D10.4.4.
I'd also tried editing the constructor of the DrupalMediaEditing as described in #5. It allows the 'data-caption' attribute through again, but as @mstrelan says, the user is unable to edit the caption. However if I save the page then edit it again, I'm then able to edit the caption as usual.
A quick way to see the problem is to edit function getSelectionResponse() in \core\modules\media_library\src\MediaLibraryEditorOpener.php
It only sets two attributes in the AJAX response it sends to the browser:
'data-entity-type' => 'media', 'data-entity-uuid' => $selected_media->uuid(),
If you use Media Library to insert an image into CK Editor then look at the source, you'll see those two attributes have been inserted, to give something like:
<drupal-media data-entity-type="media" data-entity-uuid="262ef6ef-92de-486f-bd86-1dba6c092518"> </drupal-media>
In theory we can set any of the attributes listed in #2 above in the AJAX response, so add two more in function getSelectionResponse() to give:
'data-entity-type' => 'media', 'data-entity-uuid' => $selected_media->uuid(), 'alt': 'Alt text', 'data-caption': 'Caption text',
Use Media Library to insert an image again and check the source. You'll see a new "alt" attribute has been added, but there's no sign of the "data-caption".