Add link attributes and target natively.

Created on 31 July 2023, 11 months ago
Updated 5 December 2023, 7 months ago

Add link attributes natively to the link formatter module

If there's enough interest in adding attributes to the image Link Formatter module, it should be reasonably simple to add them using the patch I'll attach shortly.

Note: The hook_update_N will need to be called because the patch includes adding a file to config/install/image_link_formatter.config.yml, a image_link_formatter.module file and a new image formatter twig file.

Give it a good look over and if it's worthwhile, by all means add it to this module.

💬 Support request
Status

Needs review

Version

2.1

Component

Code

Created by

🇨🇦Canada awasson

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Issue created by @awasson
  • 🇨🇦Canada awasson

    Patch added.

  • 🇨🇦Canada awasson

    Just adding the related issue from my issue queue at Image Link Attributes.

    Cheers,
    Andrew

  • 🇨🇦Canada awasson

    Uploaded another patch. I noticed that there was a line of troubleshooting code in the original.

    Make sure that you run update.php after patching the module so that you get the updates to the module config, which contain the attributes and targets.

    Cheers,
    Andrew

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update 11 months ago
    8 pass, 4 fail
  • 🇨🇦Canada awasson

    New Patch to remove the coding standards messages.

  • 🇮🇳India MukhtarM

    Applied the patch on #5 and its working fine.
    Adding screenshots. thanks

  • Assigned to awasson
  • Status changed to Needs review 11 months ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update 11 months ago
    8 pass, 4 fail
  • 🇨🇦Canada awasson

    @MukhtarM, Thanks for reviewing and testing the patch. Hopefully the maintainer will have a moment to review this and weigh in on it as well.

    I'm updating the issue with a new patch that includes hook_update_N() to make drupal install the config file that contains the target attributes and those for the class and rel attributes. This way, anyone who is using the existing module won't have to uninstall and reinstall the module in order to get the attributes. All they'll need to do is update the module and run the database updates.

    I've also changed the status to Needs Review and assigned the issue to myself.

    Cheers,
    Andrew

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update 11 months ago
    8 pass, 4 fail
  • 🇨🇦Canada awasson

    Here we go again...
    Attaching another patch with a schema file that will hopefully pass testing and changing to Needs Review. I'm not sure about the other tests but the majority of the failures appear to be the result of not including a schema file.

  • Status changed to Needs work 11 months ago
  • 🇨🇦Canada awasson

    I think the reason why this is failing tests is because my patch doesn't accommodate for Responsive Image Link wrappers... Yet.

    I'll have to do some more work and submit another patch.

  • Status changed to Needs review 11 months ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update 11 months ago
    8 pass, 4 fail
  • 🇨🇦Canada awasson

    The attached patch provides for Responsive Image formatting with attributes for target, class & rel.

    Hopefully this will allow the patch to pass the tests. If not, I'll have to reach out to the maintainer to see if he can assist.

    Cheers,
    Andrew

  • Issue was unassigned.
  • 🇫🇷France DYdave

    Hi Andrew (@awasson),

    Thank you very much for your interest in the Image Link Formatter module and for raising this feature request, it's greatly appreciated.
    Thanks a lot also for all the code contributions, in particular, all the patches, comments and great idea suggested in the issue summary.
     

    Add link attributes natively to the link formatter module

     
    I think this use case should have been covered already with the second version of the module, in the 2.1.x branch, see the documentation here:
    https://git.drupalcode.org/project/image_link_formatter/-/tree/2.1.x?ref...

    Link field settings

    It should integrate fine with modules altering the link field, such as Link Attributes widget or Link target , since attributes are carried over and displayed in the link wrapping the image.
    For example, if the field is configured to allow user to select to open the link URL in a new window, then this setting will also be applied to the image link.

    Corresponding to the line of code at:
    https://git.drupalcode.org/project/image_link_formatter/-/blob/8f7562b9a...

    In other words: The feature you are suggesting in this ticket and implemented in the patch is already currently available out-of-the-box (immediately), without any code, by installing and configuring contrib module Link Attributes widget .

    I have given it a quick test and here are a few screenshots below of the configuration of the Link Attributes form Widget formatter and the resulting HTML in page, displayed with the Image Link Formatter module:

    Link Attributes form Widget formatter configuration example:

     

    Resulting HTML displaying a link with attributes wrapped around an image displayed with image_link_formatter.

     

    I'm not seeing any attribute from your patch that couldn't be obtained through simple back-end configuration (no code solution), with the existing base of contrib modules.
    Additionally, if more attributes are needed, the Link Attributes Widget formatter module seems to provide advanced API features allowing to easily extend its features by adding any number of custom attributes.
     

    Which therefore brings the following questions:

    Is there any particular reason you would like this feature implemented natively in the image_link_formatter module?

    Would there be anything wrong in using the Link Attributes widget module to provide these features?
    (seems very popular with more than 40K reported installs and very complete with tests, doc and all)
     

    To be straight, from a maitainer's perspective, I think you'd understand that less code in the module means less maintenance work, less tests, less doc, less support requests, etc...
    On top of that, as detailed above, it seems there is actually a valid method of obtaining the requested feature, "out-of-the-box", which seems like an acceptable solution to this use case at the moment.
    In terms of maintainer's/coder's perspective, it does make sense to keep both sets of features separated, so each maintainer can focus on a smaller and more specific code base (git repo).
     

    I don't know what you think and if that would make sense to you, but in any case, I would greatly appreciate your feedback.
    I'm not against getting the patch committed, but if possible I'd like to hear more valid points that would justify the patch is absolutely necessary and would bring something new, that couldn't necessarily be obtained without code, at the moment.

    I hope I was able to provide some elements of answer, but feel free to let us know if I missed or overlooked anything, I would be very happy to take another look at the feature or patch.

    Once again, thank you very much for your help, testing, feedback and contributions, it's grealty appreciated.
    Cheers!

  • 🇫🇷France DYdave

    Hi @hockey2112,

    Thanks a lot for your feedback on this issue.

    This image and link are both part of a paragraph, which I have added to a block. Could that be the issue? What else would cause the link target to be ignored?

    I have just done a test locally, with a fresh D10 install :

    • Drupal core:10.1.6
    • image_link_formatter:2.1.1
    • link_attributes:2.1.0
    • paragraphs:1.16.0

     
    Everything seems to work just fine, see below the resulting HTML:

    What else would cause the link target to be ignored?

    Maybe there could be some kind of overrides in your theme or other modules.
    I would recommend you perhaps try on a fresh Drupal core install without any potentially conflicting modules, or try changing the theme on your current project and try to take the debugging from there.

    I hope this answer will help, but in any case, feel free to report back if you manage to find the problem with your project.
    Thanks in advance !

  • 🇺🇸United States hockey2112

    I switched my theme to Olivero, and sure enough the link attributes we displayed. I then changed to Bootstrap Barrio base theme (which I use in conjunction with my Barrio child theme), and the link attributes were not displayed. So it appears it must be an issue with Barrio. Very strange, because I use this module in pretty much all sites that I build and it has always worked with those.

  • 🇨🇦Canada awasson

    @hockey2112, It sounds to me like the bootstrap theme is overriding the FieldFormatter class in Image Link Formatter or overriding the Field Formatter template in Image Link Attributes.

    If the theme has a twig template for the image formatter and the theme uses some kind of variation on template_preprocess_image_formatter() then that would do it. The them may also just be overriding the Core ImageFormatter and the one inside Image Link Formatter.

    Andrew

Production build 0.69.0 2024