Link field display defaults to trim at 80 chars, breaking longer plaintext URLs

Created on 18 December 2019, over 4 years ago
Updated 10 June 2024, 16 days ago

Problem/Motivation

When you create a new link field, the display settings default to trim the output at 80 characters. This means that any time a URL longer than 80 characters is used, without a title value, the link is broken because it is trimmed.

I've hit this a number of times on sites where I forgot to remove it and ended up with broken links. It's an easy fix, but I don't think that having this as the default makes sense as it's setting up site builders to have broken links.

I wasn't able to find any documentation on why the trim_length is set to 80 characters.

Proposed resolution

Don't set a trim by default.

By default, the field should work with whatever input is provided, and users can customise the display if needed. Trimming links with a title may make sense, but I can't think of why you would ever trim a URL and even for titles it seems better for users to set the trim that suits them, rather than starting withs arbitrary (I think?) setting.

This change would only affect new link fields, not existing ones.

Steps to reproduce

* Create a link field and enable URL only and Show URL as plain text in the formatter
* Use the link

https://www.example.com/content/articles/archive-longer-long?author=John&year=2012#com

, the output should then show a broken (trimmed) url http://www.example.com/content/articles/archive-longer-long?author=John&year=20โ€ฆ

Remaining tasks

User interface changes

None.

API changes

None.

Data model changes

None

Release notes snippet

The URL is not anymore trimmed, when only plain url was choosen.

โœจ Feature request
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component
Linkย  โ†’

Last updated 6 days ago

Created by

๐Ÿ‡ฆ๐Ÿ‡บAustralia pameeela

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

Merge Requests

Comments & Activities

Not all content is available!

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

  • ๐Ÿ‡ธ๐Ÿ‡ฎSlovenia borutpiletic

    I can confirm what #33 described. I have seen the same issue in a custom template where { content.field_link.0['#url_title'] } was used and it resulted in a trimmed URL being shown.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States angrytoast Princeton, NJ

    I can reproduce the issue with a fresh site install on 10.2 on the standard profile.

    Adding a link field to the default Article content type and then configuring it on the content type's default display view mode shows the 80 character trim regardless of if it is the "Link" or "Separate link text and URL" option. It looks like this is defined as such in the respective plugins:

    • <a href="https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php#L79-87">LinkFormatter::defaultSettings</a>
    • <a href="https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/link/src/Plugin/Field/FieldFormatter/LinkSeparateFormatter.php#L28-34">LinkSeparateFormatter::defaultSettings</a>

    Digging into git history, it looks like this was added way back in 2013 at the early stages of Drupal 8 development. The issue doesn't have any clear reasons on why 80 characters is the default: https://www.drupal.org/project/drupal/issues/1796316 โ†’

    Back to the main topic: I agree that the default trim length causes more confusion than the good it does. This is especially relevant in REST views when you try to output a link, if your sample data doesn't result in a trim, it is hard to tell this will become a problem. I think removing it from the plugin defaults is a good idea.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Prashant.c Dharamshala

    Prashant.c โ†’ made their first commit to this issueโ€™s fork.

  • Pipeline finished with Failed
    6 months ago
    Total: 519s
    #73620
  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Prashant.c Dharamshala

    1. The trim_length property is of integer type

    trim_length:
          type: integer
          label: 'Trim link text length'

    therefore default value is required in integer not string.

    2. Assigned dafault value to

    'trim_length' => 0
    

    in LinkFormatter.php and LinkSeparateFormatter.php

    3. Changed the '#min' => 1 to '#min' => 0 .

    4. It solves the truncate issue and we can assign the 0 from the UI as well in the manage fields.

    5. Raising the MR and attaching the patch as well for 11.x

    Thanks

  • Status changed to Needs work 6 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    This kind of change will need test coverage.

  • First commit to issue fork.
  • Pipeline finished with Success
    4 months ago
    Total: 513s
    #93955
  • Status changed to Needs review 4 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany lmoeni

    I fixed the failing test and adjusted the link formatter test with an longer url and empty 'trim_length' value.
    Can someone take a look at this?

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia divya.sejekan

    Verified with the latest MR . In FE view the issue looks fixed and also the links redirect to correct page. Keeping it in Review state for further reviews

    Testing steps:
    1. Add Link type to any CT
    2. Set the link display as separate link and text in display setings
    3. Create a node and verify in FE

  • Status changed to RTBC 4 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    I git blamed this all the way to #501434: Move Link/URL field type into core โ†’ where it appears the 80 was brought in when link module was merged in. Didn't see any discussion around it.

    Believe making the change makes sense

    Verified that existing link fields are unaffected by the change so no upgrade path needed.

    New fields it works.

  • Status changed to Needs work 4 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom longwave UK

    Added some questions on the MR. I think this change is a good idea, even better that it only changes new fields and not existing ones.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Keshav Patel

    Keshav Patel โ†’ made their first commit to this issueโ€™s fork.

  • Pipeline finished with Failed
    4 months ago
    Total: 17053s
    #115442
  • Status changed to Needs review about 2 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany tobiasb Berlin

    The questions on the MR was answered.

  • Pipeline finished with Success
    about 2 months ago
    Total: 1141s
    #164800
  • Status changed to RTBC about 1 month ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Tested this out on a standard install of 11.x
    Added a link field to the Article content type
    Didn't do anything special in the field settings
    Went to view display and see that the link is not trimmed.

    I git blamed the 80 trim all the way to #1796316: Convert Link/URL widgets / formatters to plugin system โ†’ from 12 years ago and don't see any discussion around the use of 80 so believe the update should be fine and good quality of life improvement.

  • Status changed to Needs review 25 days ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    This only affect link fields when the plaintext option has been selected. I'm not sure that the 80 trim default is a bad default when you a displaying a link instead of plain text. I think the trim option should be disabled when you select plaintext because it is only in combination with the plaintext option that the trim option becomes destrcutive.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    So is the current proposal

    That 80 is left as is

    If you check the two boxes like so

    Then the trim should disappear?

  • Status changed to RTBC 16 days ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia onkararun

    @smustgrave when i Tested this on a standard install of 11.x and added a link field to the basic content type and i didn't do anything in the field settings then i got trimmed url and when i did changes in the field settings link check the 'Url only' and 'Show URL as plain text' with trim at 80 chars, in that case i also got the trimmed url.
    But alexpott when i tested the merge reuqest MR !6068 then i didn't get any trimmed url. And it's working fine.

  • Status changed to Needs work 16 days ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    The problem here is we're dealing with with a formatter that does two very different things.

    • One is display the link in an anchor tag. Here the trim makes sense because you are not breaking the link.
    • The other is to display the link as raw text - here the trim makes no sense whatsoever because you are breaking the link.

    HEAD currently prioritise displaying as an anchor tag and this change prioritises displaying as a raw text. I think we should make the settings make sense depending on how you've chosen to display the link (raw vs anchor).

Production build 0.69.0 2024