Linkit Link field: File entities not processed like other entities, resulting in double-encoding

Created on 15 March 2023, over 1 year ago
Updated 28 March 2024, 8 months ago

Problem/Motivation

When using the Linkit plugin for link fields, creating a link to a file/media element with a space in the filename results in a double encoded space and an invalid URL.

Steps to reproduce

  • Install Drupal 9.5.4 - standard installation
  • Install Linkit 6.0.x-dev
  • Edit default linkit profile to allow files or media in the Manage Matchers menu
  • Upload a file/media with a space in the name
  • Edit the basic page content type, add a link field with default settings but change the form display to use the linkit widget
  • Create a page using the basic page content type and link to the file/media with the link field.
  • On the edit page the link will appear like this: /sites/default/files/2023-03/name%20with%20space.pdf
  • When the page is saved and viewed the link will appear like this: /sites/default/files/2023-03/name%2520with%2520space.pdf

Proposed resolution

Remove the double encoding of spaces.

Remaining tasks

πŸ› Bug report
Status

Fixed

Version

6.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States johns996 Marquette, MI

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

Comments & Activities

  • Issue created by @johns996
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    Thanks for the thorough report. I've added this to the issues that need to be resolved for the next release ( 🌱 LinkIt Release Roadmap and Issue Prioritization Active ).

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    After some testing, I conclude this is a larger issue than simply double-encoding in files. There are related problems with both file entities and media entities. In the case of file entities, the widget save process never recognizes it as a file entity to begin with and just saves an internal:/

     path. The Linkit formatter therefore doesn't even process the URL, which results in the double-encoding.
    
    In the case of Media entities, they save correctly the first time, but if the entity form with the link field with the Linkit widget is revisited, it munges the output from something like <code>/media/1

    to entity:/media/1, which still "works" on render, but breaks the autocomplete functionality if you click on the link URL in the widget.

    I'll either rework this issue to further encapsulate the problem or create a separate issue that would end up resolving the issues with spaces in filenames.

  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    The attached patch handles the Drupal file entity, which is anomalous compared to other entities, separately, to ensure it can be 'found' as an entity and its URL rendered like other entities.

  • πŸ‡ΊπŸ‡ΈUnited States johns996 Marquette, MI

    The patch applied cleanly to my installation and fixed the issue. I tested it in both the CKEditor instance of a link field and a field using the linkit widget. Nice job Mark! Thanks for your wok on this and everything you've done to keep this great module going.

  • Status changed to Fixed over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    Thanks for your work on this and everything you've done to keep this great module going.

    Sure thing! Always happy to contribute to the community.

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

  • Status changed to Fixed about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States jodelamotte

    I'm experiencing a double encoding problem. I'm using Linkit version 6.0.0-rc1 Drupal Version 9.5.10, and although I've reviewed the patch and it appears to include all the necessary changes, the issue of double encoding persists.

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    I'm experiencing a double encoding problem. I'm using Linkit version 6.0.0-rc1 Drupal Version 9.5.10, and although I've reviewed the patch and it appears to include all the necessary changes, the issue of double encoding persists.

    Can you confirm that the original steps to reproduce in this issue cause the problem for you? If you aren't getting this problem with those steps, can you provide alternate steps to reproduce that are causing the issue for you? Thanks!

  • πŸ‡ΊπŸ‡ΈUnited States patrickfgoddard

    Can patch be modified for linkit versions 6.1 and up? We are using 6.1 and are encountering this double-encoding issue.

  • πŸ‡¨πŸ‡¦Canada laura.j.johnson@gmail.com Toronto

    We are also encountering this issue with 6.1.3 using the Linkit widget with a core formatter, in this case the Separate link text and URL formatter. And it's also with file entities.

    If core link formatters expect a decoded url then it seems ideal that the Linkit widget should take care of decoding the url. I'm not sure if there is a better way to accomplish that but here is a patch that adds an urldecode() step in MassageFormValues() in LinkWidget.

    Steps to reproduce:

    install Linkit 6.1.x-dev
    Upload a file/media with a space in the name
    Edit the basic page content type, add a link field with default settings but change the form display to use the linkit widget, change the display to use a non-linkit formatter like Separate link text and URL
    Create a page using the basic page content type and link to the file/media with the link field.
    On the edit page the link will appear like this: /sites/default/files/2023-03/name%20with%20space.pdf
    When the page is saved and viewed the link will appear like this: /sites/default/files/2023-03/name%2520with%2520space.pdf

  • πŸ‡¨πŸ‡¦Canada laura.j.johnson@gmail.com Toronto

    I opened a new issue here πŸ› Linkit Link field: Double-encoded file links Active for visibility.

Production build 0.71.5 2024