Problem/Motivation

modules/media/js/plugins/drupalmedia/plugin.js automatically turns the media caption editable in CKE.
But in our case we don't want that because we don't want to allow users to provide an embed-specific caption (and if they enter one, it will not be used in front-office).

Steps to reproduce

  1. Create a CKE field with the "Insert a media" button and filter.
  2. Create a media with a caption (it must have a figcaption HTML tag in its template.
  3. Insert this media in the CKE field.
  4. The caption is editable.

Proposed resolution

There should be a setting in the text format to disable this behaviour.

Remaining tasks

JS is not my forte but I can try to submit a patch.

Issue fork drupal-3217260

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

  • 9.5.x Comparecompare
  • 1 hidden branch
  • 3217260-add-a-way Comparechanges, plain diff MR !1893

Comments

prudloff created an issue. See original summary.

prudloff’s picture

Status: Active » Needs review
StatusFileSize
new2.49 KB

The attached patch adds a boolean setting (enabled by default) on the filter.

(I guess another solution could be to disable captioning when the Caption filter is not enabled.)

gauravvvv’s picture

StatusFileSize
new2.49 KB
new909 bytes

Fixed custom command failed. Attached interdiff for the same.

pameeela’s picture

Category: Bug report » Feature request
Issue tags: +Bug Smash Initiative

Thanks @prudloff for reporting and @Gauravmahlawat for the patch. Updating the category because adding a new feature is a feature request rather than a bug :)

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

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should 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.

joshua1234511’s picture

Version: 9.3.x-dev » 9.4.x-dev
Status: Needs review » Needs work

Tested the feature request with the steps mentioned in Issue summary.
Applied the Patch provided #3
Tested on v9.4.x-dev
- Patch successfully applied
- Checkbox provided to enable disable the the Caption

The JS still needs some work, the entire caption field freezes

Code Review.
editor.config.DrupalMediaLibrary_enableCaptioning Not defined.
This needs to be set and passed in getConfig

/**
   * {@inheritdoc}
   */
  public function getConfig(Editor $editor) {
    if ($editor->hasAssociatedFilterFormat()) {
      $filters = $editor->getFilterFormat()->filters();
      if ($filters->has('media_embed')) {
        $filter_caption = $filters->get('media_embed')->settings->enable_captioning;
      }
    }
    return [
      'drupalMedia_previewCsrfToken' => \Drupal::csrfToken()->get('X-Drupal-MediaPreview-CSRF-Token'),
      'DrupalMediaLibrary_enableCaptioning' => $filter_caption ?? 0,
    ];
  }

media schema needs to be updated

    enable_captioning:
      type: boolean
      label: 'Enable captioning'

joshua1234511’s picture

Create a issue fork, Applied the patch and updated the code
- This will enable/disable the caption checkbox entirely form the form.
https://git.drupalcode.org/project/drupal/-/merge_requests/1893/diffs#c9...
- Passed the variable value for DrupalMediaLibrary_enableCaptioning
https://git.drupalcode.org/project/drupal/-/merge_requests/1893/diffs#86...
- Updated the schema

Pending/Todo
The Js code still needs some review/fix

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.

cgmonroe’s picture

Another way to do this is to just change the editable caption selector to include a check for a data attribute on the figcapture tag. E.g.:

        editables: {
          caption: {
            selector: 'figcaption:not([data-no-edit])',
            allowedContent: 'a[!href]; em strong cite code br',
            pathName: Drupal.t('Caption')
          }
        },

This :not([data-no-edit']) part means that if the twig template uses:

<figcaption data-no-edit="1" .... >

The caption will not be editable.

cgmonroe’s picture

With a little research, I found a way to modify the editable caption selector without patching core.

Basically, you need a custom bit of JS to run when CKEDITOR is initialized. This can be in your theme (the one used for editing) or added via a custom module. Here's the basic JS to run:

Drupal.behaviors.customCKEditorConfig = {
  attach: function (context, settings) {
    if (typeof CKEDITOR !== "undefined") {
      // When each CKEDITOR instance is created on the form page.
      CKEDITOR.on('instanceCreated', function (e) {
        var instance = e.editor;
        // Run code when the instance is initialized.
        instance.on('instanceReady', function (e) {
          var instance = e.editor;
          // Change caption selector
          if ('drupalmedia' in instance.widgets.registered) {
            instance.widgets.registered.drupalmedia.editables.caption.selector = 'figcaption:not([data-no-edit])';
          }
        });
      });
    } 
  }
}

Update your template with figcapture to include the data-no-edit attribute to prevent caption editing.

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.

GaëlG made their first commit to this issue’s fork.

gauravvvv’s picture

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.

gaëlg’s picture

With CKEditor 5, included in Drupal 10 core, it looks like media caption is already not editable. CKE4 is unsupported and needs a contrib module.

prudloff’s picture

Status: Needs work » Closed (outdated)

CKE5 does allow editing captions but only if the "Caption images" filter is enabled.
So site builders can choose if the caption can be edited.