Problem/Motivation

The module is being useful but I just realized that it still doesn't have compatibility with responsive images

Comments

Nelo_Drup created an issue. See original summary.

Webbeh’s picture

Issue tags: -responsive image styles, -responsive images link formatter

  • DYdave committed 8f7562b on 2.1.x
    Issue #3294374 by DYdave: Added support for Responsive images with new...
dydave’s picture

Thank you very much @Nelo_Drup for your interest in the Image Link Formatter module and for raising this issue, it is greatly appreciated.

Thanks a lot for creating this feature request as it sparked a great revamp of module's architecture, dropping support for earlier versions and leveraging latest Drupal/PHP standards.

When starting investigating this issue we didn't realize Responsive Images were now fully part of Drupal core (have been for a long time) and therefore it would definitely make sense integrating image_link_formatter with it.

After doing some basic tests locally, just trying to extend core ResponsiveImageFormatter, we found out the module could work out-of-the-box just duplicating and renaming the scripts, as a new module 'responsive_image_link_formatter'.

Therefore, to avoid code duplication, easier maintenance and Tests coverage, we went through the process of abstracting module's logic as much as possible so it could be used by other modules:
The Responsive Image Link Formatter is an example of implementation and extension of a link field wrapped around an image formatter.

Based on the work carried in tickets:

 
Added support for Responsive images with new formatter 'responsive_image_link_formatter' integrated with core 'responsive_image'.
In order to avoid an additional dependency on image_link_formatter, the new formatter was implemented in its own sub-module image_link_formatter:responsive_image_link_formatter which depends on core module responsive_image.
 

Added Formatter plugin: ResponsiveImageLinkFormatter ('responsive_image_link_formatter'), which is essentially a copy of ImageLinkFormatter, with the minumum amount of code needed to be able to extend core ResponsiveImageFormatter, inject the entity field manager and use trait ImageLinkFormatterTrait to integrate with a custom link field.

It is important to note this only works, without any additional logic ("out-of-the-box"), because the core responsive_image module's formatter plugin uses the same property as the core image formatter: 'image_link' (see for example formatter settings dropdown select), to provide a link rendered on the image through element's '#url' property. In particular, the output of the link depends on the way it is rendered through extended image formatter (theming, template files, etc...).

Otherwise, it could perhaps be possible in some cases to override certain methods from the trait and implement a custom logic, based on more or different formatter properties.
 

Added responsive_image_link_formatter Test cases:

Functional:
\Drupal\Tests\responsive_image_link_formatter\Functional\ResponsiveImageLinkFormatterTest
Extends: ResponsiveImageFieldDisplayTest

Kernel:
\Drupal\Tests\responsive_image_link_formatter\Kernel\ResponsiveImageLinkFormatterTest
Extends: ImageFormatterTest

Mostly copies of corresponding Functional\ImageLinkFormatterTest and Kernel\ImageLinkFormatterTest with different parameters to call trait's test routines:

  • Functional: doTestImageLinkFormatterWrappedImage with the HTML expected from the output.
     
  • Kernel: doTestImageLinkFormatterUrlOptions currently without options:
    It is important to note Kernel tests ('Kernel\ResponsiveImageLinkFormatterTest') currently don't properly test the attributes in the markup rendered by the responsive_image_link formatter due to a bug in core module responsive_image, see issue with patch:
    #3064751: Responsive image formatter does not support URL/Link options
    Therefore, the test currently just checks there is a link, but without any attributes (empty).
     
    In other words, because of this issue, any additional attributes passed with the link, for example, '_target', 'title', etc... is ignored when displayed through the responsive_image_formatter, and thus for responsive_image_link_formatter.

 
Lastly, the inline and Doc comment blocks were greatly improved to provide more information on the test cases added or the new formatter plugin class and its methods.

The changes were committed to branch 2.1.x at:
https://git.drupalcode.org/project/image_link_formatter/commit/8f7562b

Feel free to let us know if you have any questions, comments, concerns or feedback on any of the recent code changes or the ticket in general, we would certainly be happy to help.
Thanks in advance!

nelo_drup’s picture

#4 Thank you very much I managed to do incredible things with drupal and this module helped me a lot in that

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.