If the field uses this field_formatter contains no value for the redirection, we just render the image.

I want the image to be link to its content if the link field is empty.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Thytane’s picture

Status: Active » Needs review
FileSize
724 bytes
DYdave’s picture

Hi @ThyFighter,

Thank you very much for your interest for the Image Link Formatter module and posting this feature request with a patch.

I have tested the patch and it works as you would expect, however, I think this logic would perhaps be a little too specific and probably fulfills your requirements but others might come up with different needs, such as not linking the image or linking it to its file instead of its content.

It isn't the first time I've seen a request related with conditional behaviors of display formatters based on field values, as it was previously posted at #1965090: Hiding The Image When The Link Field Is Empty where someone else came up with the need for hidding the image field if the link field is empty.
A convenient solution based on Field formatter conditions seemed to have solved the issue and allowed user to configure a condition to hide the image when the link would be empty.

Therefore, it got me thinking that perhaps, instead of using this patch, a more appropriate solution would be to go through another module, such as FFC which adds configuration settings to field formatters to execute certain actions for certain conditions.

For hiding an image when a link would be empty, user was able to use module's features out-of-the-box, since this condition/action already exists and is implemented in the latest versions of FFC.
However, since the condition/action required in this case, doesn't exist in the module yet, a new feature was proposed at #2292947: Add a new condition: Link image field to content or file if target is empty, with a patch against ffc-7.x-1.x and another contrib module, allowing to add the condition Link image field if target field is empty.

In short, another potential solution could be:

  1. Install FFC.
  2. Apply the patch OR install the module ffc_extra, as explained at #2292947-1: Add a new condition: Link image field to content or file if target is empty.
  3. Go to the image field display formatter and select the condition: Link image field if target field is empty.
  4. Select Content for the Link to dropdown field.
  5. Save the form.

Please see the attached screenshot of the setup of the Image Link Formatter's FFC configuration form.

Now, I could certainly understand this method could seem more complicated, from your standpoint, than adding 4 lines of code to the image_link_formatter.module. I am aware it would imply installing additional modules, with more configuration settings and maintaining more code. Not to mention issues related with FFC and various potential compatibility issues with Panels, Views, etc...

However, this method perhaps covers more potential use cases, not only allowing to link the image field to its content, but also to its file. Additionally, the linking behavior is not enforced by any module and can be controlled by users through the interface.
For developers with more advanced requirements, the code from #2292947-1: Add a new condition: Link image field to content or file if target is empty could also demonstrate FFC API's flexibility and be a starting point for any further extensions or modifications.
Lastly, by using FFC and FFC Extra, no actual modification of any existing module would be necessary (you can either use ffc_extra or the patch).

I would certainly greatly appreciate your feedback on this suggested approach, especially if it could work in your case and solve the problem.

Feel free to let me know if you would have any questions, objections, concerns, or issues, on the patch or the module from #2292947-1: Add a new condition: Link image field to content or file if target is empty, the configuration settings or this suggested solution in general, I would surely be glad to provide more information or explain in more details.

Thanks again very much for the patch, posting this issue and in advance to all for your testing, comments and reviews.
Cheers!

DYdave’s picture

rcodina’s picture

Hi @DYdave,

I understand why you suggest to use FFC module. However, having to install another module for doing a simple thing like this is overkill. I already have a lot of modules enabled and this lead to more performance loss. Also, that module have far less users than this one and this one seems more mantained.

Would be great that this module would allow to choose desired behaviour when field link is empty wihtout being to much specific. In my case, I need to not link the image when the link is empty. My current solution is to use the patch above and modify it to my specific needs. I won't install FFC.

rcodina’s picture

I share my patch. With this one, if link field is empty, the image won't be a link.

@DYdave I also changed and "isset" for a "!empty" to make it work. I think this is a key change that must be done.

Status: Needs review » Needs work

The last submitted patch, 5: link_image_to_his-2174139-5.patch, failed testing.

rcodina’s picture

@DYdave As I said in #5 the key is to replace "isset" with "!empty". Then, with no additional changes in your module it works for my needs. Also I think this behaviour is the one you already have but it doesn't work for all cases. Then maybe you should consider commiting this.

rcodina’s picture

rcodina’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 7: link_image_to_his-2174139-7.patch, failed testing.

rcodina’s picture

@DYdave Do you know why tests say patch cannot be applied? It works like a charm for me.

DYdave’s picture

Status: Needs work » Needs review

Module doesn't have tests.
Resubmitting same patch for manual review