Ability to set default caption value

Created on 8 February 2024, about 1 year 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',
      ],
    ];
Feature request
Status

Active

Version

10.2

Component
CKEditor 5 

Last updated about 9 hours ago

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @nyanmar
  • Status changed to Postponed: needs info about 1 year 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 about 1 year 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 about 1 year 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 about 1 year ago
  • 🇨🇦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
  • 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 🇧🇪🇪🇺
  • 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">&nbsp;</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".

Production build 0.71.5 2024