TL;DR - when using Media WYSIWYG, if you change the View Mode on the image, it adds classes rather than replacing them which creates problems if there's a conflict in the rules, as is the case with some Panopoly CSS.

Something that I thought for a long time was a config problem on my end. I just tested it on a clean install of Panopoly 1.15 simplytest.me and originally posted in the Panopoly queue, but moved here since it seems like it's coming from here somewhere, but this may have to be moved again.

The Problem:

If you change view modes, you end up with conflicting classes on your images. Rather than replacing the old classes based on view mode and image style class, it retains the original ones and adds the new ones.

This is a particular problem if the image is first inserted using Quarter Size or Half Size because the Panopoly CSS gives them widths and max-widths. So if I start at Quarter Size and change to Thumbnail, I get a highly pixelated quarter-size image.

That first image style class will never change, so if you are using the Panopoly CSS straight up, it leads to problems.

Obviously it would be great to have this fixed upstream so it updates properly, but for now I'm just overriding the Panopoly CSS, which is a bit of a challenge in itself because those widths are defined with !important.

Steps to Recreate

  1. Spin up a clean install of Panopoly
  2. Edit a page and add an image. Select the Quarter Size view mode in the Display As select dropdown. Save. You'll now have the image class twice and the view mode class once (see code samples below)
  3. Edit the page again, click on the image, click the image editing icon, change the view mode to Thumbnail and save. Note that image is pixelated and that classes are getting added, not replaced (again see code below)
  4. Do it again and change it to Original
  5. The image now has the original image class and the last one, as well as all three view mode classes (see code samples below)

I have also had situations where the image style doesn't change, which is how I first noticed this. If you have an image style that's small (say a thumbnail) and you change the view mode to something larger, I end up with the actual thumbnail image style and then CSS classes that display it larger (and obviously pixelated). This latter problem I have NOT seen on a clean install with panopoly, so it's probably something in my setup.

Code Samples

After the steps listed above, here is the image tag

<img class="panopoly-image-quarter media-element file-teaser file-preview file-default panopoly-image-original" src="http://s5bbe65dd8c0c740.s3.simplytest.me/sites/default/files/styles/panopoly_image_original/public/j0400264.jpg?itok=wZdFSpLg" alt="">

Note that all three view mode classes are there (file-preview, file-default, file-teaser) and the last image-style classe (panopoly-image-original) and the first one (panopoly-image-quarter).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ergophobe’s picture

Title: Image and View Mode classes doubled and accumulate on inserted inline image. » Image and View Mode classes accumulate when inserting inline image.
Project: Panopoly » D7 Media
Version: 7.x-1.15 » 7.x-2.x-dev
Component: WYSIWYG » Media WYSIWYG
ergophobe’s picture

Issue summary: View changes
ergophobe’s picture

Issue tags: +panopoly
dsnopek’s picture

Title: Image and View Mode classes accumulate when inserting inline image. » View mode classes accumulate when changing image view mode

At least part of what you are describing comes from Panopoly: it's panopoly_images_preprocess_image_style() that's adding the image style class.

However, even if you comment out that bit of code, you'll still accumulate classes for the file entity view modes, for example, I got this in my testing: <img class="media-element file-teaser file-preview file-default" ...>

I'm not sure what can be done here generically in Media: it needs to preserve any classes added in the WYSIWYG using WYSIWYG tools. I haven't dug into the code yet, but don't know if it'd be possible to identify which ones came from Drupal originally and remove only those when switching view modes. :-/

But I do think we could for sure fix this in Panopoly! We know which classes should and shouldn't be there for a particular view mode and maybe we could filter them out in preprocess function somewhere?

So, if we can't find a generic solution in Media, I'd be OK with moving this back to Panopoly and hard-coding a solution that works for us...

ergophobe’s picture

The issue with the view modes is coming, I think, from media/modules/media_wysiwyg/js/media_wysiwyg.filter.js lines 264ff of 7.x-2.0-alpha4+25-dev

      if (info.view_mode) {
        classes.push('file-' + info.view_mode.replace(/_/g, '-'));
      }
      element.addClass(classes.join(' '));

This is adding classes, but not clearing the original class.

Ideally it would grab a list of view modes, remove any classes based on view modes, and then add in the one class for the current view mode.

Does anyone know how to grab a list of view modes in Javascript? I can do it in PHP, but have no clue how to do it in Javascript.

dsnopek’s picture

You'd probably have to grab the list in PHP, then pass it to Javascript via drupal_add_js('setting', array(...)). The Media WYSIWYG code is already passing a number of things from PHP to Javascript, so there's probably somewhere to just add the view modes, but I haven't actually dug into the code to find where that is.

chrisgross’s picture

From what I can tell, this problem appears to be caused by a media patch included with panopoly: See this issue. Without it, alpha4 does not have this problem.

rwohleb’s picture

Patch #66 on #2126697: Wysiwyg -- Alt and Title fields require some special handling. appears to correct the duplicate view-mode class issue.

brockfanning’s picture

Status: Active » Needs review
FileSize
647 bytes

I think that this fix, which is minor, belongs here its own patch. Here is the relevant part from the issue rwohleb found.

brockfanning’s picture

FileSize
660 bytes

Slight change to catch any cases of empty strings.

ultimike’s picture

I've just tested the patch in comment 10 on a local environment of a "real" site and it appears to work.

-mike

ultimike’s picture

I spoke too soon - it does not remove the "panopoly-" style classes...

-mike

ultimike’s picture

I spent about an hour digging into this, and the patch in comment 10 works for clearing out classes added from View Modes, but it doesn't address the need to also clear out classes added from Image Styles.

As dsnopek pointed out in comment 4, panopoly_images_preprocess_image_style() adds a single class to each image based on the image style machine name. The Media module (specifically, the media_wysiwyg.filter.js file) doesn't know (or care) about these classes, and therefore leaves them alone. So, when a view mode is changed, the class originally added (based on the image style) isn't removed. I _think_ that this only happens when the image is first added, in my testing, I don't think classes based on image styles are added on any subsequent view mode changes (I'd love for someone to double-check me on this).

I'm surprised more folks haven't found this to be an issue. I was just able to reproduce this issue on a fresh install of Panopoly, so it appears to be a valid issue still.

I think solving this in Panopoly Images is a good idea, but I'm unsure as to which preprocess function we'd be able to leverage to fix this...

thanks,
-mike

brockfanning’s picture

@ultimike, this patch specifically looks for file-* classes, with the assumption that they will be the view mode classes. Maybe a better solution would to add an alter hook, like maybe "hook_media_wysiwyg_class_prefixes_alter", that allowed other modules to indicate additional prefixes to act on, such as panopoly-*.

dsnopek’s picture

@ultimike: Can you open a new Panopoly issue for this? Your problem seems to be on the Panopoly side of the fence, where this issue is attempting to address the Media part. That said, I thought we fixed having the extra image style classes in #2422163: Panopoly CSS causes issues with WYSIWYG image because of image style class names which was merged over a year ago. :-/ This added some code to panopoly_images_preprocess_image_style() which attempted to remove the excess classes, but apparently it's not working in your case...

ultimike’s picture

brockfanning’s picture

I'm testing out the latest dev version, and it doesn't look like this is a problem anymore. Unless I'm missing something I think we can close this.

brockfanning’s picture

Status: Needs review » Closed (cannot reproduce)

I'll close this for now. Please re-open if this is still a problem.