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
Create a patch that does this + tests
User interface changes
None.
API changes
None.
Data model changes
None
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #44 | AfterFix2.png | 197 KB | divya.sejekan |
| #44 | AfterFix1.png | 135.96 KB | divya.sejekan |
| #44 | BeforeFix.png | 73.66 KB | divya.sejekan |
Issue fork drupal-3101714
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3101714-link-field-display
changes, plain diff MR !6068
Comments
Comment #2
pameeela commentedWell, now I can't reproduce this in vanilla Drupal! Will note the steps next time, maybe it's an issue with core + contrib.
Comment #3
tobiasbNo this a bug. I will provide a patch.
Comment #4
tobiasbComment #5
tobiasbChange also settings summary.
Comment #6
tobiasbComment #7
kapilv commentedComment #9
kapilv commentedComment #10
kapilv commentedComment #12
tobiasbOk. I added a test.
Comment #13
shobhit_juyal commentedTo give more clarity about the new patch, I've Attached the interdiff between #9 and #12.
Comment #14
tobiasbComment #16
tobiasbComment #18
tobiasbComment #19
tobiasbComment #20
pameeela commentedThis needs updated steps to reproduce as the steps I originally posted are not valid (which is why I closed it).
It's not clear to me that this is an actual bug, we need to confirm that first.
Comment #21
beanjammin commentedI just ran headlong into this one. If it's not a bug it's at least a major usability issue.
Comment #22
pameeela commentedI can’t reproduce it on a fresh install. I don’t remember what site it occurred on but there must be something wrong somewhere, perhaps in a template.
It definitely happened since I reported it, but it can’t be fixed in core without steps to reproduce it in core.
Comment #23
beanjammin commentedIn my case the 80 character trim configuration was in place for some instances of the link field, but not in others and not in new ones that I created once I was aware of the issue. I will try to reproduce the issue on a fresh install.
Comment #24
tobiasbI added the section "Steps to reproduce".
Comment #25
tobiasbAdded the state change again and also fix formatter summary.
Comment #26
tobiasbComment #28
vlyalko commentedHello, thank you for looking at this issue.
I applied your patches for the core 9.3 and none of them worked.
Tried #18 and #25. Problem is still there.
UPDATE: This issue is only happening (with or without the patch applied) with "Separate Link text and URL format" is selected for the link in the form display. When just Link format is selected, the issue is gone. But there will be some updates needed in the twig file.
Comment #29
vlyalko commentedComment #31
vlyalko commentedThis patch will NOT truncate URL. If URL TITLE is NULL, it will use truncated URL as a TITLE.
Comment #33
pameeela commentedI can only reproduce this when using a custom template and it's erroneously referencing the link title instead of the URL.
E.g.
{{ content.field_link.0['#title'] }}if there is no title, this will give you the value of the URL, trimmed. But if you just update this to be{{ content.field_link.0['#url'] }}you get the full URL.Comment #35
borutpiletic commentedI 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.Comment #36
angrytoast commentedI can reproduce the issue with a fresh site install on 10.2 on the
standardprofile.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:
LinkFormatter::defaultSettingsLinkSeparateFormatter::defaultSettingsDigging 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.
Comment #37
mahtab_alam commentedComment #40
prashant.c1. The
trim_lengthproperty is of integer typetherefore default value is required in integer not string.
2. Assigned dafault value to
in
LinkFormatter.phpandLinkSeparateFormatter.php3. Changed the
'#min' => 1to'#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.xThanks
Comment #41
smustgrave commentedThis kind of change will need test coverage.
Comment #43
lmoeniI 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?
Comment #44
divya.sejekan commentedVerified 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
Comment #45
smustgrave commentedI 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.
Comment #46
longwaveAdded 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.
Comment #48
tobiasbThe questions on the MR was answered.
The default value/schema should be fixed in other issue. e.g. url_plain/url_only should be FALSE.
trim_length should be NULL, nullable in config schema, and int >= 1.
Comment #49
smustgrave commentedTested 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.
Comment #50
alexpottThis 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.
Comment #51
smustgrave commentedSo is the current proposal
That 80 is left as is
If you check the two boxes like so
Then the trim should disappear?
Comment #52
arun.k commented@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.
Comment #53
alexpottThe problem here is we're dealing with with a formatter that does two very different things.
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).
Comment #54
mherchelI would argue that although the trim works within an anchor tag, it should definitely not be the default. On practically all sites I work on I have to modify this default (on each and every link field on each display mode). If we were implementing this new, there's no way we would choose to truncate by default (although it should still be an option)
Comment #56
hosterholzSince I merged changes from 11.x, the current branch can no longer be applied to Drupal 10.x oder 11.1.x. Here is a patch.
Comment #57
dcam commentedHonestly, as I've been working on the Link module I've been thinking that I wish the plain text URL settings had been implemented as a completely separate formatter. I don't know that there's a way out of it now.
Comment #58
tobiasbWe can keep a default value for trimming, perhaps set it to a higher number.
Slack does trim a URL link text in form of
http://www.example.com/content/articles/archive?[...]year=2012&month=february#comThis could be new option for URL only without entered title and disabled plain text.
The origin bug, can not be fixed when we just remove the default settings.
What I saw while fixing the test. We have a test which can not be configured via UI nor the formatter support it. (Case:
url_only=false, url_plain=true)I could remove it.
Comment #59
tobiasbComment #60
smustgrave commented@alexpott thoughts?
Comment #62
smustgrave commentedCan this one get a rebase please.
Comment #63
tobiasbMR rebased.
Comment #64
fabrondeau commentedHello,
Why do we have to trim the link url ?
As I can understand the formatter text "Link text trimmed to @limit characters", thought it would trim the link title ('#title'), and not the link url ('#url_title').
Who would agree this proposal ?
As I read the tests, I can check the results that only the text is trimmed, not the url value. hey seems to be OK.
See the files:
Comment #65
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #66
tobiasbComment #67
smustgrave commentedMaybe this could be a change we just land in 12 if there's disruption concerns? But appears to be working as advertised.
Comment #68
quietone commentedI read the comments and the MR and updated credit. I didn't find any unanswered questions.
I also manually tested this and can confirm the change works.
A reminder to anyone testing a UI issue an uploading before and after screenshots. Those images need to be available from the issue summary to help reviewers and committers. Thanks.
Comment #69
quietone commentedComment #72
catchCommitted/pushed to main and 11.x, thanks!
While this is a bugfix it's a fairly obvious interface change for existing sites so I didn't backport it to 11.4.x - if someone feels strongly we could re-open but I given the issue has been open for seven years for me it's more important to get it into a release than a specific release.
Did my best with commit credit but this is quite a long issues so apologies for any omissions.