[drupalMedia] Unable to set default caption value on <drupal-media> tag

Created on 8 February 2024, 5 months ago
Updated 15 March 2024, 3 months ago

I wish to set a default caption when I insert a media into the WYSIWYG using the core Insert Media button. I have a custom media library opener that extends CoreMediaLibraryEditorOpener where I set the default attribute values of media such as align, alt, and caption. In CK4 I was able to pass those values and they were working. However, in CK5 while the align and alt gets set properly - data-caption does not. I made sure that the caption filter has been turned on in the text format as well. I confirmed the captions that were set previously in CK4 are there and working. Is there any way to get this to work for CK5?

public function getSelectionResponse(MediaLibraryState $state, array $selected_ids) {
    $selected_media = $this->mediaStorage->load(reset($selected_ids));

    $response = new AjaxResponse();
    $values = [
      'attributes' => [
        'data-entity-type' => 'media',
        'data-entity-uuid' => $selected_media->uuid(),
        'alt' => 'custom alt text',
        'data-align' => 'center',
        'data-caption' => 'custom caption here',
      ],
    ];
🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
CKEditor 5 

Last updated about 2 hours ago

Created by

Live updates comments and jobs are added and updated live.
  • JavaScript

    Affects the content, performance, or handling of Javascript.

  • 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.

  • Issue created by @nyanmar
  • Status changed to Postponed: needs info 4 months ago
  • 🇧🇪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 4 months ago
  • 🇧🇪Belgium Wim Leers Ghent 🇧🇪🇪🇺

    >2 weeks of silence: I'm assuming the snippet I provided in #2 unblocked @nyanmar.

  • 🇦🇺Australia mstrelan

    I was able to get the data-caption attribute inserted in the source by adding it in the this.attrs and this.converterAttributes in the constructor of the DrupalMediaEditing 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 3 months ago
  • 🇦🇺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 the DrupalMediaCaptionEditing should take care of that but it just needs to be triggered some how.

  • Status changed to Needs review 3 months ago
  • 🇵🇰Pakistan jibran Sydney, Australia

    Updated the title and re-categories the issue. Let's update the IS with steps to reproduce.

  • Status changed to Needs work 3 months ago
  • 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.)

  • 🇧🇪Belgium Wim Leers Ghent 🇧🇪🇪🇺
Production build 0.69.0 2024