Problem/Motivation

The DrupalMedia plugin, added in 8.8.0, does not allow for CKEditor styles to applied to <drupal-media> elements.

Steps to reproduce

  1. Install with 'Standard' install profile.
  2. Enable Media and Media Library modules.
  3. Set default view mode for 'Image' media bundle to render an image element with no label.
  4. Add 'Insert from Media Library' and 'Styles' buttons to 'Basic HTML' toolbar.
  5. Add the following dropdown style: 'drupal-media.test-class|Test'.
  6. Save the filter format.
  7. Upload an image to the Media library and insert into a Basic Page node.
  8. Observe the only class on the <drupal-media> element is 'cke_widget_element'.
  9. Select the media element and use the Styles dropdown to add the 'test-class' class.
  10. Observe the only class on the <drupal-media> element remains 'cke_widget_element'.
  11. Switch to CKEditor’s source mode.
  12. Observe no class attributes.
  13. Manually add a 'test-class' class.
  14. Switch out of source mode.
  15. Observe the only class on the <drupal-media> element is 'cke_widget_element'.
  16. Switch back to source mode and observe the class has been removed.

Proposed resolution

Update the DrupalMedia plugin to allow for <drupal-media> elements to be assigned classes by the StylesCombo plugin.

Remaining tasks

  • Write patch
  • Update tests
  • Community review

User interface changes

<drupal-media> elements can now be assigned classes by the StylesCombo plugin.

API changes

None.

Data model changes

None.

Release notes snippet

TBD.

Comments

Chris Burge created an issue. See original summary.

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.

vsujeetkumar’s picture

Status: Active » Needs review
StatusFileSize
new24.75 KB

I am trying to reproduce the issue with the above mentioned steps in 9.1.x however fail to add class in dropdown according to step 5(Add the following dropdown style: 'drupal-media.test-class|Test'.) and got the below error, Can someone help in this,

"The provided list of styles is syntactically incorrect."

bkosborne’s picture

I suspect the problem here is that the "drupal-media" element is actually a CKEditor "widget", and the procedure for declaring a style is compatible with a widget has some extra steps.

chris burge’s picture

Status: Needs review » Active
Related issues: +#2911527: Allow dashes in Styles dropdown's element names

@vsujeetkumar - The issue you're experiencing is #2911527: Allow dashes in Styles dropdown's element names. Give patch #57 a try.

Also, we have no patch, so setting back to 'Active'.

nicholasthompson’s picture

StatusFileSize
new41.94 KB

I've made a little module like this:

function media_editor_widget_styles_editor_js_settings_alter(array &$settings) {
  foreach ($settings['editor']['formats'] as &$format) {

    $format['editorSettings']['stylesSet'][] = [
      'name' => 'Float Right',
      'type' => 'widget',
      'widget' => 'drupalmedia',
      'attributes' => [
        'class' => 'float-right',
      ],
    ];
  }
}

And I get a style option in the dropdown, however it doesn't seem to apply to the element...

I think I might need to add something to the widget config? I've read through media/js/plugins/drupalmedia/plugin.js but I'm not familiar with CKEditor plugins...

I can see `allowedContent` contains an empty classes hash...

patrickmichael’s picture

On Drupal 8.9.1. I have patch #57 from #5 applied. Attempting to add the style text-muted|Faded secondary text to custom styles results in the following error:
The provided list of styles is syntactically incorrect.
Am I missing something?

nicholasthompson’s picture

Patrick - you might need a . (dot) before text-muted otherwise it's only going to apply to <text-muted> elements.

.text-muted|Faded secondary text

patrickmichael’s picture

Thank you Nicholas, for pointing me in the right direction. I have a custom theme based on Classy that uses the Bootstrap 4 library. I am adding Bootstrap text formatting and typography classes to the styles drop down. I did not differentiate between inline styles and styles that need to be applied to a block element.

Inline styles:
cite|Cite
code|Code
var|Variable

Block styles:
small.text-muted|Text Muted
h1.display-1|H1 Display 1
footer.blockquote-footer|Blockquote Footer

Thanks again.

phenaproxima’s picture

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.

jddh’s picture

I think this is a really good idea -- it is currently rather difficult to apply any basic styling to Media in a CKEditor field, which is a rather fundamental requirement for using Media at all.

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.

phma’s picture

Issue tags: +JS
StatusFileSize
new1.79 KB

The media widget discards any classes added by CKEditor on 'downcasting'. That's why using a custom module like in #6 doesn't work.

This patch should fix this.

phma’s picture

Status: Active » Needs review
phma’s picture

StatusFileSize
new1.94 KB

Fixing code styling issue and using the latest branch.

vsujeetkumar’s picture

@phma Interdiff is missing in #16, It will help to understand what the changes has been done.

And this patch needs Reroll, as it has changes in core/modules/media/js/plugins/drupalmedia/plugin.es6.js file.

Update es6 file then compile the code with yarn.

gauravvvv’s picture

StatusFileSize
new1.79 KB
new433 bytes

Fixed CS errors.

lan’s picture

Hi,
I have added the patch to the site, I am able to apply the class to the style dropdown at CK editor, but the class not able to add to the image source after drupal-media tag.

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.

kathyjg’s picture

I have same issue as Lan, I have applied the patch, I can add the class to the style dropdown options, however when I select the style for my embedded media document it does not apply it. I just want to provide an option for my client to be able to bold or feature certain documents that are embedded into a page with their media library.

akhildev.cs’s picture

Hi, tried patch #18,
I am also able to add 'test-class' from dropdown styles.I added an image from the media library and then selected 'test class' from the style dropdown. But class is not applied to the added media.
CKEditor source mode shows only '<'drupal-media'>' after the patch is applied.

wim leers’s picture

abhijith s’s picture

StatusFileSize
new14.19 MB

Applied patch #18 but its not adding class attribute to drupal-media tag.

After patch:
after

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.

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.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new144 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

wtrv’s picture

Version: 10.1.x-dev » 9.5.x-dev
StatusFileSize
new1.98 KB

Re-rolled patch against 9.5.x

wtrv’s picture

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

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.

sclsweb’s picture

I tested the patch from #28 with CKEditor 4, using a style configured as "drupal-media.img-thumbnail|Imageborder," and it did not add the class to drupal-media.

alinouman’s picture

I'm also looking a solution for it.

alinouman’s picture

I fixed it in a different way

/**
 * Implements hook_form_alter().
 */
function modulename_form_alter(&$form, FormStateInterface $formState, $formId) {
  $step = $formState->get('step');
  if ($formId === 'entity_embed_dialog' && $step === 'embed') {
    // Retrieve the saved 'entity_class' value from the embed attributes.
    $entityClass = $formState->get('entity_element')["data-entity-class"] ?? 'none';
    // Add a new field to the form for class assignment.
    $form['entity_class'] = [
      '#type' => 'radios',
      '#title' => t('Entity Class'),
      '#description' => t('Choose a class to be added to the entity.'),
      '#default_value' => $entityClass,
      '#options' => [
        'none' => t('None'),
        'wysiwyg-padding-right' => t('Padding Right'),
        'wysiwyg-padding-left' => t('Padding Left'),
      ],
    ];
    // Change the form submit handler.
    $form['#submit'][] = 'modulename_media_entity_embed_submit';
  }
}

/**
 * Custom submit handler for the media_entity_embed_dialog form.
 */
function modulename_media_entity_embed_submit($form, FormStateInterface $formState) {
  // Get the value of the 'entity_class' field.
  $entityClass = $formState->getValue('entity_class');
  // Store the entity class in the embed button attributes.
  $formState->setValue(['attributes', 'data-entity-class'], $entityClass);
}
wim leers’s picture

Title: Allow CKEditor styles for <drupal-media> » [upstream] Allow CKEditor styles for <drupal-media>
Status: Needs work » Postponed
Issue tags: +Needs upstream feature

This will never get fixed for CKEditor 4.

For CKEditor 5, this is blocked on https://github.com/ckeditor/ckeditor5/issues/14274#issuecomment-1744624793.

wim leers’s picture

marc.groth’s picture

For those wanting to implement this sooner... This module seems to be very promising: https://www.drupal.org/project/ckeditor5_inline_styles

bkosborne’s picture

Title: [upstream] Allow CKEditor styles for <drupal-media> » Allow CKEditor styles for <drupal-media>
Status: Postponed » Active

I don't think this is blocked upstream. The upstream issue indicates that each plugin that provides its own block level widget needs to be updated to integrate with the Styles plugin, like they had to do for Table widgets. There's no action in the upstream issue we're waiting on. I think we just need to update our custom plugin's code to allow for this.

bkosborne’s picture

I had to do this for a custom plugin of mine. I wrote up some important tidbits on how to do it here.

chris burge’s picture

When I asked around among colleagues today, I was pointed to this blog post: https://dev.acquia.com/blog/drupalelementstyle-add-styles-drupal-media-c....

@bkosborne - Is this solution functionally equivalent to the plugin you authored?

mortona2k’s picture

The instructions in that post are working, I was able to add data-attribution to the wrapper for a remote video.

This ticket looks relevant: https://www.drupal.org/project/drupal/issues/3406020

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.