Error when image does not have a view mode and handle different view modes

Created on 17 August 2023, over 1 year ago
Updated 29 August 2023, about 1 year ago

Problem/Motivation

For my WYSIWYG I have the following configuration:

As you can see, I've enabled multiple view modes. My "Default" view mode does not employ an image style, but "Card Image" does have one set.

This causes an error in the service as the code performs isset($field_settings['settings']['image_style'] which does exist, but it is an empty string. This causes an issue as the service tries to find a compatible plugin with the empty string. You will see the error in your logs if you click the crop button.

Furthermore, I tried switching the view mode to "Card Image" and that also did not work. It seems that the code will only build a crop for the default view mode and does not respect the selection within CKeditor5.

See example https://www.drupal.org/files/issues/2023-08-17/Recording%202023-08-17%20... β†’

πŸ› Bug report
Status

Fixed

Version

1.3

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States devkinetic

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

Comments & Activities

  • Issue created by @devkinetic
  • πŸ‡ΊπŸ‡ΈUnited States devkinetic
  • πŸ‡«πŸ‡·France DrDam

    Did you have choose a view mode which use a crop in the CK5 settings ?

    I think, I need to find another way to manage that, but it's here that you choose the "croping view mode"

  • πŸ‡ΊπŸ‡ΈUnited States devkinetic

    My setting looks like yours. My "Default" view mode does NOT use a crop. Could it be as simple as adding a check to make sure the string isn't empty? The code right now assumes that an image has a image style applied and that won't always be true.

    I was able to work around passing the selected view mode to the dialog in CK5 JS. I'll be committing that code in a second.

  • πŸ‡«πŸ‡·France DrDam

    My setting looks like yours. My "Default" view mode does NOT use a crop.

    That's the point, currently, you need select a view mode which use a crop.

    How this would work with some-one enable to understand how code the f*** ck5 plugin js :

    Main Use Case
    State 1 : The "contextual crop" button is disactivated
    State 2 : When the media is rendered in the wysiwyg, we catch the view_mode used for the rendering
    State 3 : An ajax call check if the view_mode use a image style with a crop
    State 4 : On ajax response :
    4.1 if YES (view_mode => image style => crop) : activate "contextual crop" button, and on click, passe to the modal the view_mode, when the modal are closing, add info in wysiwyg
    4.2 if NO : the "contextual crop" button stay disactivated, a tool-tip explain why

    Use Case Bis
    State 5 : The user change the view_mode
    State 6 : Goto State 3

  • πŸ‡«πŸ‡·France DrDam

    In fact, I think, I have something :

    in the media node in wysiwyg, there is a "data-view-mode" attributs when user don't use the view mode "configured as defaut" in text format.
    This attribut is not here when it's the "configured as defaut" which use.

    So I just need get the "configured as defaut" view_mode, and update the js to send it & the "data-view-mode" to my modale ... (installing node & yarn ..)

    I'll check that tomorow

  • πŸ‡ΊπŸ‡ΈUnited States devkinetic

    I agree with you, disabling the button when you have an view mode selected that does not support cropping. Through my debugging I found drupalMediaElement['_attrs'].get('drupalElementStyleViewMode') which will contain the selected view mode in the CK5 plugin. If it is undefined, it's using the default from the filter configuration. We can model the ability to disable based on:

    if(drupalMediaElement['_attrs'].get('drupalElementStyleViewMode') !== undefined) {
    
    }
    
  • πŸ‡«πŸ‡·France DrDam

    I got it too

  • πŸ‡«πŸ‡·France DrDam

    I just push an update on 1.3.x-dev

    The modal will now use the view mode of the media IN the wysiwyg.

    If the view_mode has not crop, a message explain that in the modal.
    if you change the view mode in the wysiwyg, that the view_mode use in the modal.

    Tomorow, i'll clean the code, and "deconstruct" all element not needed in the conf.

    I'm go to bed, thank for pushing me even further.

  • Status changed to Fixed over 1 year ago
  • πŸ‡«πŸ‡·France DrDam

    Release in 1.3.2

  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States devkinetic

    I'm testing this today. This somewhat works, but needs a few tweaks.

    1. When there are no plugins, LoadPlugins returns NULL. In implementation, it's always assumed that it will return an array of plugins or an empty array when none are found. This causes array countable errors. There are three places this method is used, we should review them. The simplist thing to do would be just make the method return an empty array instead of NULL.
    2. When using the crop dropdown, we don't actually want that crop to show within the modal, just the end result. This would be after you select your focal point and press save to close the modal. The image shown in the modal should be the untouched source image.

    To expand upon the second bullet further, think of it this way. I have an image that is 16:9 originally, and I want to use it as a cropped 4:3 using focal point in the WYSIWYG. After the update, if I select that 4:3 image style in the dropdown and press the crop button, the modal shows an already cropped image with the focal point. I can no longer see the entire image as the left and right have been trimmed away. Essentially, it should work like it does for a media field where you can see the whole image.

    We have made progress because now the modal can know if should allow cropping or not, but it went one step farther actually using the crop in the modal itself.

  • πŸ‡«πŸ‡·France DrDam

    1 => OK

    2 => The problem comes from the fact the "thumbnail" style (default for Focal Point widget) may use the cop...
    see : β†’

    When use set it in entity form mode, you have the message : "Do not choose an image style that alters the aspect ratio of the original image nor an image style that uses a focal point effect."

    In fact, I don't know what can I make more than a message in the crop modal

  • πŸ‡«πŸ‡·France DrDam

    in Todo list :
    - what appen if you use a responsive image with some image with IWC & FP

  • πŸ‡ΊπŸ‡ΈUnited States devkinetic

    On a media ref field, when I edit the crop of an image, it uses the "media_library" image style, but when I edit the crop of an image within WYSIWYG it uses "thumbnail" image style.

    I inspected my focal point widget settings and I can confirm I have everything set up to use the "media_library" image style. So perhaps the issue is now that focal point is falling back to default. It should instead be using the widget display settings.

    On my site the "thumbnail" image style does use focal point, as it is used elsewhere on other entities.

  • πŸ‡ΊπŸ‡ΈUnited States devkinetic

    I also noticed that since the latest update my "crop" settings within text formats is empty. Not sure if this was intentional, as one would assume this would be where I would set that to be my "media_library" image style.

  • πŸ‡ΊπŸ‡ΈUnited States devkinetic

    I see that the "thumbnail" image style is currently hardcoded in \Drupal\media_contextual_crop_fp_adapter\Plugin\MediaContextualCrop\FocalPoint::getComponentConfig

    Overriding that to "media_library" seems to do the trick in my case. I am able to apply a crop. At this point, I then tested swapping back to the "default" view mode within WYSIWYG, but an error is thrown as the default does not use an image style, but it is expected as the element now has "crop settings" from me just previously to that setting them on the other image style.

  • πŸ‡ΊπŸ‡ΈUnited States devkinetic
  • πŸ‡«πŸ‡·France DrDam

    I just push on 1.3.x-dev a correction :

    - reactivating the CK5-plugin settings, in order to choose an image_style which NOT USING A CROP. This image_style will be use as "preview" in the crop modal

    I push new releases for the two adapters in order to use this parameter

  • πŸ‡ΊπŸ‡ΈUnited States devkinetic

    That seems to have done the trick, along with adding an empty image style to satisfy the plugin.

    I do have to say though, we have focal point form display configuration on the media item itself, would it not make the most sense to retrieve that and apply it within the popup? This might be an issue for the focal point adapter itself. Something to think about.

  • πŸ‡ΊπŸ‡ΈUnited States devkinetic

    I just did some experimentation with this concept. Here is a quick rundown:

    \Drupal\media_contextual_crop_embed\MediaContextualCropEmbedService::addCropWidget
    \Drupal\media_contextual_crop_embed\MediaContextualCropEmbedService::addCropWidgetForCk5

    // get the image component for the media item
    $source_field_config = $media_library_form_display->getComponent($source_field_name);
    // modify getComponentConfig to pass it, instead of using preview_image_style from the plugin config
    $component_data = $plugin->getComponentConfig($media_embed_element, $crops, $source_field_config);
    

    \Drupal\media_contextual_crop_fp_adapter\Plugin\MediaContextualCrop\FocalPoint::getComponentConfig

    if (isset($source_field_config['#type']) && $source_field_config['#type'] === 'image_focal_point') {
      $preview_image_style = $source_field_config['settings']['preview_image_style'];
      $preview_link = $source_field_config['settings']['preview_link'];
    }
    return [
      'type' => 'image_focal_point',
      'settings' => [
        'progess_indicator' => 'throbber',
        'preview_image_style' => $preview_image_style ?? 'crop_thumbnail',
        'preview_link' => $preview_link ?? TRUE,
        'offsets' => $default_values['data-crop-settings'] ?? '30,30',
      ],
    ];
    

    Obviously you would have to adjust MediaContextualCropPluginBase.php and revert the last round of commits that introduced the "preview image style" on the ck plugin config.

    The only question now is, right now it is grabbing the default form display, but in reality, we would really want the "media_library" display. But it would be nice to be able to choose. Maybe that is what the ck plugin config should allow you to select if anything. On the service side, we'd just have to update to:

    $media_library_form_display = $this->entityDisplayRepository->getFormDisplay('media', $media->bundle(), 'media_library');
    

    Or if configurable, it would be:

    $media_library_form_display = $this->entityDisplayRepository->getFormDisplay('media', $media->bundle(), $data_source['display_mode']);
    
  • πŸ‡«πŸ‡·France DrDam

    I do have to say though, we have focal point form display widget configuration on the media item itself, would it not make the most sense to retrieve that and apply it within the popup? Then it matches up 1:1 with how we configure each media type. This might be an issue for the focal point adapter itself. Something to think about.

    I can not do that, because if you use both focalPoint & ImageWidgetCrop, the only "point I have" to determine the crop widget you need are the "image style" use at the end ( media view-mode => field_image => image style).

    So, now you can choose the "default" image style in the modal

    => On release 1.1.1 of media_contextual_crop_fp_adapter, the "getComponentConfig" will be updated for that :

  • Status changed to Fixed over 1 year ago
  • πŸ‡«πŸ‡·France DrDam

    Fixed in 1.3.3

  • Status changed to Fixed about 1 year ago
Production build 0.71.5 2024