Problem/Motivation

When inserting image as a link, the inserted code is empty, while it should be a link to an image, using the file title/alt as a link text.

Steps to reproduce

  1. Create an image upload field
  2. Enable option to insert it as a file link
  3. Edit content, upload an image
  4. To to insert a link to it - clicking "insert" will do nothing.

Proposed resolution

Update insert-link.html.twig:

Before:

{%
  set attach = {
    id: id,
    attributes: {
      title: [
        'description',
      ],
    },
    content: [
      'description',
      'filename',
    ],
  }
%}
<a href="{{ url }}"{% if class %} class="{{ class }}"{% endif %}{{ attributes }} data-insert-type="{{ field_type }}" data-insert-attach='{{ attach|json_encode() }}'></a>

After the fix:

{%
  set attach = {
    id: id,
    attributes: {
      title: [
        'description',
        'title',
        'alt',
      ],
    },
    content: [
      'description',
      'title',
      'alt',
      'filename',
    ],
  }
%}
<a href="{{ url }}"{% if class %} class="{{ class }}"{% endif %}{{ attributes }} data-insert-type="{{ field_type }}" data-insert-attach='{{ attach|json_encode() }}'></a>

Once the template is updated, an image from image field can be insert as a link to an original file and it works as long as the image has alt or title specified.

Comments

pawel.traczynski created an issue. See original summary.

snater’s picture

Category: Feature request » Bug report
Status: Active » Needs review
StatusFileSize
new381 bytes

Thanks for submitting the issue. It appears to be a bug, because it should be possible to insert an image as a link without having to apply any changes. The idea of the insert-link.html.twig template though is to be specific for files, in the sense of being agnostic to image specifics. If image specific attributes are to be picked up, the idea would be to create a custom template override per (admin) theme.

However, there was supposed to be a proper default with the link text being the file name. This does not work at the moment. The attached patch fixes the issue, so now the file name should be picked as the link text by default.

  • Snater committed 65f2394f on 3.x
    Issue #3402668 by Snater: Insert image field image as a link
    

  • Snater committed 0a220182 on 8.x-2.x
    Issue #3402668 by Snater: Insert image field image as a link
    
snater’s picture

Status: Needs review » Fixed

Fixed in 8.x-2.1 and 3.0.0-beta3.

Status: Fixed » Closed (fixed)

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