I've just noticed that it's not possible to select a "Responsive image style" as the "Colorbox image style" when configuring this module. Is that because this module relies on the use of a formatter (as does D8 core's Responsive image module)? It would be great to get this working.

CommentFileSizeAuthor
#69 interdiff-68-69.diff5.19 KBjurgenhaas
#69 colorbox-responsive-image-2808883-69.patch29.13 KBjurgenhaas
#68 interdiff-2808883-64-68.txt9.34 KBhuzooka
#68 colorbox-responsive-image-2808883-68.patch30.93 KBhuzooka
#64 colorbox-responsive-image-2808883-64.patch29.8 KBinterdruper
#63 colorbox-responsive-image-2808883-63.patch29.65 KBs_leu
#61 interdiff_46-61.txt511 bytesJ-Lee
#61 colorbox-responsive-image-2808883-61.patch29.68 KBJ-Lee
#48 2808883-48.patch29.65 KBQusai Taha
#46 reroll_diff_34_46.txt9.18 KBArantxio
#46 colorbox-responsive-image-2808883-46.patch29.67 KBArantxio
#45 colorbox-responsive-image-2808883-45.patch31.41 KBArantxio
#44 colorbox-responsive-image-2808883-44.patch32.91 KBArantxio
#43 colorbox-responsive-image-2808883-43.patch32.89 KBArantxio
#29 reroll_diff.txt3.22 KBJ-Lee
#29 colorbox-responsive-image-2808883-29.patch29.2 KBJ-Lee
#27 colorbox-responsive-image-2808883-27.patch29.14 KBJohan den Hollander
#25 25_formatter.png61.4 KBJohan den Hollander
#25 25_interdiff.txt5.58 KBJohan den Hollander
#25 colorbox-responsive-image-2808883-25.patch15.82 KBJohan den Hollander
#23 23_colorbox-html_colorbox_responsive_image_style.png23.22 KBarturopanetta
#23 23_colorbox-html_responsive_content_image_style.png141.44 KBarturopanetta
#23 23_colorbox-entity_view_display.png42.24 KBarturopanetta
#21 image_format.png7.49 KB4kant
#20 2808883-20.patch27.78 KBSuresh Prabhu Parkala
#16 colorbox_responsive-2808883-16.patch27.7 KBJ-Lee
#12 colorbox_responsive-2808883-12.patch29.88 KBtoasdt
#6 responsive_images-2808883-6.patch13.11 KBAnonymous (not verified)
#4 2808883-4_responsive_field_formatter.patch16.24 KBrolfmeijer

Issue fork colorbox-2808883

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

danbohea created an issue. See original summary.

oana.hulpoi’s picture

Hey, Do we have any news regarding this feature request?
Of course, we can always adopt a custom and personal solution to achieve this. But, as long as responsive images are in core, I think that it would be nice to have a colorbox module built-in solution...
Thanks!

Anonymous’s picture

Assigned: Unassigned »
rolfmeijer’s picture

Status: Active » Needs work
FileSize
16.24 KB

I had a go with it this weekend, but unfortunately could not finish it and I won’t be able to work on it the coming weeks. I post it here, maybe it is useful as a starting point, otherwise I will look into it later again. I made a separate Field Formatter, basically combining the Colorbox Formatter with the Responsive Image Formatter form core.

A few remarks:

  • I added a FieldFormatter and template, I changed the colorbox theme function as well, but because rendering is not working yet, I’m unsure if there is more work here.
  • For simplicity, I left out the first-image options.
  • Something goes wrong with the cache tags, so rendering is not functioning as yet (but shouldn’t be too hard).
Anonymous’s picture

@rolfmeijer - great start. I had been working on something similar, but was having a few little hiccups. Your patch has gotten me past those.

After getting a little further along, I'm starting to realize, we need to make a few decisions. Since in 8.x, colorbox inline is not a default option, we can't use a responsive image for the actual colorbox content, since you would need a singular image url.

What we could do is allow responsive image style selection for the content image, and set up the "for Colorbox" field only have "None (original image)". And then add functionality in colorbox_inline to allow responsive image styles in the "for Colorbox" field, and then in the template, render out the responsive image in a hidden container inline, and link to the the image's container.

Anonymous’s picture

I've got things at a more usable state.

As I mentioned in my previous comment, colorbox_inline will need to be used if one wants to use responsive images in the colorbox as standalone, it is really only possible to use them for the node/trigger images.

MrPaulDriver’s picture

It would be great to see this finished off.

LonitaD’s picture

I would love if we could get this working. When I tried testing it by selecting the new formatter in the display settings, I got the following PHP error:

Error: Call to a member function label() on null in /modules/colorbox/src/Plugin/Field/FieldFormatter/ColorboxResponsiveFormatter.php on line 270

The loading spinner appeared next to the formatter selector but the settings didn't load and I got an AJAX error and it kept spinning.

auxiliaryjoel’s picture

Hi I was just wondering if there was any more progress on this?

markdc’s picture

Our company is looking for this. If funding is needed, send me a message.

Neslee Canil Pinto’s picture

Status: Needs review » Needs work

Patch failed to apply

toasdt’s picture

This patch should

– apply (both to the latest commit d2fa6de5 and version 8.x-1.6) and work and
– solve the issue in comment #8.
– Additionally, it's adapted to some changes in the module code since the first patches three years ago, like coding standards or naming conventions. For example, the names and structure of the Colorbox Responsive formatter are now closer to the Colorbox formatter and/or the Responsive Image formatter (in core's responsive_image module).

But...

– I haven't tested the colorbox_inline integration yet, which is necessary for responsive images in the Colorbox popup, see comments #5 and #6, in particular the preparations for that in colorbox.theme.inc (look for the $colorbox_inline variable). By now, the "Colorbox Responsive" formatter only affects the "content image" (the preview in the content).
– The first-image options is still left out (as in #4).

RedTop’s picture

Thanks for this, I'll get onto this now. :)

Edit:

Colorbox Inline functionality is not yet functional, a modal opens but collapses. I am on Drupal 8.8.6 and patched against 8.x-dev d2fa6de5, using the Colorbox Responsive Formatter on Media Image entities.

After uninstalling Colorbox Inline the modal works but is obviously the full image. Note that the Media Entity Manage Display form also shows the option to set the 'Colorbox responsive image style' when Colorbox Inline is not enabled. The availability of this should probably be dependent on Colorbox Inline being available or not.

Regardless, thanks! It's a big step forward. :)

I had started working on this as well so I will compare code and see if there is something useful left.

jurgenhaas’s picture

Status: Needs review » Needs work

Tried this with media image from core and it works great for responsive content image style. For the colorbox responsive image style, it doesn't seem to work, I always get the original image being displayed in the colorbox.

J-Lee’s picture

Short review from patch from #12.

+  if (!empty($variables['responsive_image'])) {
+    $variables['responsive_image']['#attributes'] = $item_attributes;
+
+    // Do not output an empty 'title' attribute.
+    if (mb_strlen($item->title) != 0) {

I notice an issue with the fallback img tag. There are some attributes (e.g. alt tag value) missing.
The $variables['responsive_image']['#attributes'] are build in template_preprocess_responsive_image_formatter() in responsive_image.module with alt, title etc. In the patch they are taken from $variables['item_attributes'] or an empty array, which seems to be wrong.

J-Lee’s picture

Assigned: » Unassigned
Status: Needs work » Needs review
FileSize
27.7 KB

Reroll of #12 with the fixed issue from #15.

Still needs work with the other issues.

J-Lee’s picture

Status: Needs review » Needs work

Tests are green but putting back to needs work as of #13 and #14.

J-Lee’s picture

After thinking about #14 I found some more things, we should think about.

  1. Why so many things are done in a preprocess function and why it's not done right in the viewElement() method of the field formatter?
  2. Rename 'colorbox_responsive_node_style' to 'colorbox_responsive_preview_style'
  3. Rename 'colorbox_responsive_image_style' to colorbox_responsive_modal_style'
  4. Move the responsive image processing out of template_preprocess_colorbox_responsive_formatter() into its own method. Then it could used for the preview and the colorbox modal image.
  5. Compare both preprocess functions in colorbox.theme.inc. If a preprocess function is needed for both, move duplicate code into its own method.
4kant’s picture

Patch #16 doesn´t apply to latest colorbox release 8.x-1.7 or latest dev.

Suresh Prabhu Parkala’s picture

Status: Needs work » Needs review
FileSize
27.78 KB

It's just a re-roll.

4kant’s picture

FileSize
7.49 KB

Thanks - patch applies to 8.x-1.7.
Cleared the cache...
There is no "Colorbox Responsive" as an image formatter.
image_format

Johan den Hollander’s picture

Tested the #20 patch. The Colorbox responsive option became available only after clearing the caches.
It works well!

arturopanetta’s picture

I tested patch #20.

In the backend, the "Colorbox responsive" option works fine.

Entity view display

The output of "Responsive content image style" works correctly.

Responsive content image style

The output of "Colorbox responsive image style" does not work correctly, the original image is returned in any breakpoint.

Colorbox responsive image style

Johan den Hollander’s picture

Status: Needs review » Needs work

As mentioned before by @jurgenhaas and @arturopanetta the Cololorbox responsive image style does not work at all.

There are some preparations done, but the code is commented out.

//   elseif ($responsive_style) {
  //     $variables['url'] = '/colorbox-responsive/' . $item->entity->id() . '/' . $settings['colorbox_responsive_image_style'];
  //   }

And so the code in else runs:

else {
    $variables['url'] = file_create_url($image_uri);
  }

Which gives us the original image.

This if fine for me, for now as I want to show the original image or at least a large one to the users. But it is not responsive. And so this issue needs work!

Johan den Hollander’s picture

Always showing the original image in the colorbox popup may not be the best solution after all. Users having uploaded stock images of many MB's etc...

So I tried my luck copying and pasting some code and now found a (temporarily) solution that makes it possible to select the imagestyle that is used in the colorbox. It is not a responsive image, but just directly picking the imagestyle.
This is achieved by adding the colorbox_image_style to the settings form, and also to the output.
The chosen imagestyle is then also exported as a dependency when doing a config export.

The summary of the settingsform also reflects this setting:
screenshot of the settings.

Please review, I am not thinking this is the final solution. Just trying to get this issue moving and offering a workaround to make this functionality more usable.

Johan den Hollander’s picture

Status: Needs work » Needs review
Johan den Hollander’s picture

Reupload, #25 patch dit not apply correctly. Should be ok now.

frankdesign’s picture

Patch at #27 work perfectly for the Responsive Image Style setting for the image in the page content.

But the setting for 'Responsive image style for Colorbox' doesn't seem to do anything. The image style used for the Colorbox image uses the setting 'Image style for Colorbox' setting. In my use case, I want the original image to open in the Colorbox, so by setting 'Image style for Colorbox' to 'None (original image)' works perfectly.

Thanks

F

J-Lee’s picture

Reroll for colorbox 1.8. Needs review because of SA-CONTRIB-2022-007.

Johan den Hollander’s picture

The #29 patch applies to colorbox 1.8.0 and works the same way as #27.

4kant’s picture

Did someone get colorbox_inline to work?
Colorbox_responsive images work in general, but with colorbox_inline it´s still the same as for #13.

Thanks!

jurgenhaas’s picture

Status: Needs review » Needs work

Unfortunately, patch from #29 doesn't apply to the latest release anymore as some of the changes have already been made to the official code base. Just couldn't figured out which ones.

s_leu made their first commit to this issue’s fork.

s_leu’s picture

Status: Needs work » Needs review

Re-rolled the patch in a MR against 8.x-1.x . Also implemented the method FormatterInterface::isApplicable() in the new formatter plugin to prevent confusion/errors when selecting the formatter if the responsive_image module isn't enabled at all.

jurgenhaas’s picture

Status: Needs review » Reviewed & tested by the community

This is looking good again. Thanks a lot @s_leu

Johan den Hollander’s picture

THe #34 works well. Thanks!

Nathan Tsai’s picture

Wanting to get WebP images enabled for Colorbox.

Looking forward to seeing this patch committed :)

PROMES’s picture

I patched the current version 8.x-1.10 but no Colorbox image appears when clicking on a picture.
When will this issue be working? I need it for three sites.

PROMES’s picture

I am sorry. I made a mistake. Patch 29 works as expected. Like #38 I like to have the popup images also in WebP format.
Thanks for a great module and this patch.

J-Lee’s picture

Version: 8.x-1.x-dev » 2.0.x-dev
Status: Reviewed & tested by the community » Needs work

I think, we need a reroll against the latest 2.0 branch.

Arantxio’s picture

Status: Needs work » Needs review
FileSize
32.89 KB

Re-rolled the patch for the new 2.0.x branch, some code just seemed to overlap. so I just manually checked and implemented the old code.

Needs to be completely verified before use, as this is my first time making a patch like this.

Patch applies, and passes the tests, but it needs some refinement for coding standards, as some indentations has 4 spaces instead of 2.

Arantxio’s picture

As stated in #43 I had issues with the indentations, i have reformatted it and made a new patch.

Arantxio’s picture

The patch in #44 used tabs instead of spaces, which is also bad practice. reformatted it again.

Arantxio’s picture

I changed some more coding standards and also made reroll_diff so you can see the changes since the MR_13 in comment #34

Vrancje’s picture

Tested #46 and works on 9.3.22! Thanks a lot!

Qusai Taha’s picture

Re-roll patch for 8.x-1.10

VVS’s picture

Status: Needs review » Reviewed & tested by the community

#46

surrim’s picture

Tested colorbox-responsive-image-2808883-46.patch and does not work as espected with Colorbox 2.0.x / Drupal 10.0.0.

The Colorbox Responsive field configuartion looks like that:

Format settings: Colorbox Responsive
Responsive image style for content*: [ Lucas ][v]
Image style for Colorbox: [ Vital ][v]
Responsive image style for Colorbox*: [ Lucas ][v]
Gallery (image grouping): [ Per post gallery ][v]
Caption: [ Automatic ][v]

[ Update ] [ Cancel ]

Briefly:

  1. Responsive image style for content (responsive thumbnail image) is working as espected
  2. Image style for Colorbox is always used as Colorbox image
  3. Responsive image style for Colorbox has no effect

Included thumbnail image code:

<a href="/sites/default/files/styles/vital/public/2022/12/test.jpg.webp?itok=sqtDR5YD" title="Flowers" data-colorbox-gallery="thumbnails-3bQepcbu8gw" class="colorbox cboxElement" data-cbox-img-attrs="{&quot;alt&quot;:&quot;Flowers&quot;}" data-once="init-colorbox">  
  <img srcset="/sites/default/files/styles/lucas_11/public/2022/12/test.jpg.webp?itok=Tkr8a1Ph 199w, /sites/default/files/styles/lucas_12/public/2022/12/test.jpg.webp?itok=W5xnOzhl 322w, /sites/default/files/styles/lucas_13/public/2022/12/test.jpg.webp?itok=_QCMCiHb 521w" sizes="256px" src="/sites/default/files/styles/vital/public/2022/12/test.jpg.webp?itok=sqtDR5YD" alt="Flowers">
</a>

Included Colorbox image code:

<div id="cboxLoadedContent" style="width: 1835px; overflow: auto; height: 1032px;">
  <img alt="Flowers" class="cboxPhoto" src="/sites/default/files/styles/vital/public/2022/12/test.jpg.webp?itok=sqtDR5YD" style="width: 1835px; height: 1032px; float: none;" width="1835" height="1032">
</div>

I think Image style for Colorbox is obsolete here (only for non-responsive Colorbox) and Responsive image style for Colorbox should be used instead.

PROMES’s picture

The line (#279 in my patch):
if (((strpos($colorbox_style, 'colorbox/example') !== FALSE) || $config->get('colorbox_caption_trim')) && (mb_strlen($caption) > $trim_length)) {
gives this message:
Deprecated function: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in _colorbox_formatter_get_caption()

astringer’s picture

I concur with #50 and add that the image style formatter also does not work as expect for Colorbox 2.0.x / Drupal 9.5. See #50 for details.

Can we change this to Needs Work? To warn folks that it doesn't really work in the way you'd think.

Also it would be great to get this working, I read through this whole thread and I'm wondering if these same issues still exist. (A single URL is required for the color box image). I'm happy to help if anyone has ideas on how to approach. Would be nice to not have to serve a single large colorbox image to everyone.

KlemenDEV’s picture

Status: Reviewed & tested by the community » Needs work
KlemenDEV’s picture

Is anyone working on this? I can help with active testing as this feature is quite important on our website.

J-Lee’s picture

@KlemenDEV: I don't think anyone is actively working on it.

The MR is outdated and the patch from #46 is the last state from which it should be continued.

For the responsive image style for content (responsive thumbnail image) the lazy loading option is also missing.

J-Lee’s picture

@@ -139,6 +82,190 @@ function template_preprocess_colorbox_formatter(&$variables) {
     $variables['url'] = $file_url_generator->generateAbsoluteString($image_uri);
   }

+  if (!empty($variables['responsive_image'])) {
+    $attributes = [];
+    // Do not output an empty 'title' attribute.
+    if (mb_strlen($item->title) != 0) {
+      $variables['responsive_image']['#title'] = $item->title;
+      $data_cbox_img_attrs['title'] = '"title":"' . $item->title . '"';

$item->title could be NULL, so a deprecation will be triggert: "Deprecated function: mb_strlen(): Passing null to parameter #1 ($string) of type string is deprecated". Maybe change to if (mb_strlen($item->title ?? '') != 0) {

J-Lee’s picture

In summary, we should continue with patch #46 and respect the reviews from #50, #51 and #56.

J-Lee’s picture

Assigned: Unassigned » J-Lee

I have updated MR to #46 and fixed #51 and #56.

But there are more issues e.g.:

  • $colorbox_style = $config->get('colorbox_style') But there is no config 'colorbox_style'. Maybe it should 'custom.sytle'
  • Same with $config->get('colorbox_caption_trim_length') -> Should be 'advanced.caption_trim_length'
  • ... there are more ...
J-Lee’s picture

Assigned: J-Lee » Unassigned

The points from #58 are not relevant here.

But I think we should split this topic into several issues because there are different functions.
The original request was that Colorbox should be compatible with a responsive image formatter. We should focus on that and merge that first.

Another feature that doesn't work (cleanly) is to display a responsive image inside Colorbox. We should address this in a separate followup issue.

Perhaps the maintainers can comment briefly on the strategy?

J-Lee’s picture

I cannot change the target branch at the MR to the 2.0 branch 😞

J-Lee’s picture

s_leu’s picture

Status: Needs work » Needs review
FileSize
29.65 KB

Re-rolled this once more against the new 2.0.x branch which the patch in #61 doesn't apply any more because the README.txt is now README.md. So this new patch just moves the README part into the .md file

interdruper’s picture

Patch #63 works fine, but a deprecated notice is triggered under PHP 8.2:
Deprecated function: Creation of dynamic property Drupal\colorbox\Plugin\Field\FieldFormatter\ColorboxResponsiveFormatter::$attachment is deprecated in Drupal\colorbox\Plugin\Field\FieldFormatter\ColorboxResponsiveFormatter->__construct() (line 96 of /colorbox/src/Plugin/Field/FieldFormatter/ColorboxResponsiveFormatter.php)

This patch for 2.0.x fixes the issue.

luenemann’s picture

Status: Needs review » Needs work

No complete review!
The patch should use the service colorbox.gallery_id_generator introduced in #2935266: Make gallery id generation function independent., released in 8.x-1.7 on 5 March 2021

luenemann’s picture

Sorry, service colorbox.gallery_id_generator is in use.

But use Drupal\Component\Utility\Crypt; is missing:

+++ b/colorbox.module
index 6b216e2..23c9a87 100644
--- a/colorbox.theme.inc

--- a/colorbox.theme.inc
+++ b/colorbox.theme.inc

+++ b/colorbox.theme.inc
+++ b/colorbox.theme.inc
@@ -8,6 +8,7 @@

@@ -8,6 +8,7 @@
 use Drupal\file\Entity\File;
 use Drupal\image\Entity\ImageStyle;
 use Drupal\Component\Utility\Xss;
+use Drupal\responsive_image\Entity\ResponsiveImageStyle;
 

@@ -139,6 +82,190 @@ function template_preprocess_colorbox_formatter(&$variables) {
+    $image_id = $entity_id . '-' . Crypt::randomBytesBase64(8);
candelas’s picture

Thanks @interdruper, patch in #64 works.
I hope soon it will be part of the great Colorbox module.

huzooka’s picture

Status: Needs work » Needs review
FileSize
30.93 KB
9.34 KB

Continued the patch in #64:

  • Fixed CS of the new formatter
  • Addressed #66
  • Added schema for the new formatter.
jurgenhaas’s picture

4kant’s picture

#69 applied cleanly in Drupal 10.2.5 and latest dev-Version of colorbox.
And it works well.
Thanks!

paulmckibben’s picture

Updating issue credits.

paulmckibben’s picture

Status: Needs review » Needs work

Thanks to everyone who has contributed to this patch and gotten it to where it is so far. I just see a couple small problems:

1. With DOMPurify enabled, HTML in captions still get converted to text.
2. colorbox-responsive-formatter.html.twig should use the same aria attributes that colorbox-formatter.html.twig has.

Edit: actually, on deeper inspection, colorbox-formatter.html.twig uses aria attributes incorrectly, so disregard item 2 above.

  • paulmckibben committed 6186423b on 2.0.x
    Issue #2808883 by J-Lee, Arantxio, s_leu, Johan den Hollander,...
paulmckibben’s picture

Status: Needs work » Fixed

I fixed the problem with DOMPurify - I updated ColorboxResponsiveFormatter to load DOMPurify if it exists. Thanks to everyone for your work on this patch. It is committed!