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

  1. Create a patch that does this + tests

User interface changes

None.

API changes

None.

Data model changes

None

Release notes snippet

Issue fork drupal-3101714

Command icon 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:

Comments

pameeela created an issue. See original summary.

pameeela’s picture

Status: Active » Closed (cannot reproduce)

Well, now I can't reproduce this in vanilla Drupal! Will note the steps next time, maybe it's an issue with core + contrib.

tobiasb’s picture

Assigned: Unassigned » tobiasb
Status: Closed (cannot reproduce) » Active

No this a bug. I will provide a patch.

tobiasb’s picture

Issue summary: View changes
Issue tags: +Needs tests
StatusFileSize
new1.59 KB
tobiasb’s picture

StatusFileSize
new1.98 KB

Change also settings summary.

tobiasb’s picture

Issue summary: View changes
StatusFileSize
new1.32 KB
kapilv’s picture

Status: Active » Needs review
StatusFileSize
new54.86 KB
new21.5 KB
new1.75 KB

Status: Needs review » Needs work

The last submitted patch, 7: 3101714-7.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

kapilv’s picture

StatusFileSize
new1.95 KB
kapilv’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 9: 3101714-8.patch, failed testing. View results

tobiasb’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new4.17 KB

Ok. I added a test.

shobhit_juyal’s picture

StatusFileSize
new4.59 KB

To give more clarity about the new patch, I've Attached the interdiff between #9 and #12.

tobiasb’s picture

Issue tags: -Needs tests

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

tobiasb’s picture

Version: 9.1.x-dev » 8.9.x-dev
StatusFileSize
new4.28 KB

Status: Needs review » Needs work

The last submitted patch, 16: drupal-3101714-16.patch, failed testing. View results

tobiasb’s picture

Status: Needs work » Needs review
StatusFileSize
new4.29 KB
tobiasb’s picture

Assigned: tobiasb » Unassigned
pameeela’s picture

Status: Needs review » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative, +Needs steps to reproduce

This 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.

beanjammin’s picture

I just ran headlong into this one. If it's not a bug it's at least a major usability issue.

pameeela’s picture

I 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.

beanjammin’s picture

In 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.

tobiasb’s picture

Version: 8.9.x-dev » 9.3.x-dev
Issue summary: View changes
Issue tags: -Needs steps to reproduce

I added the section "Steps to reproduce".

tobiasb’s picture

Title: Link field display defaults to trim at 80 chars, breaking longer URLs » Link field display defaults to trim at 80 chars, breaking longer plaintext URLs
Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new5.01 KB

Added the state change again and also fix formatter summary.

tobiasb’s picture

StatusFileSize
new930 bytes

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

vlyalko’s picture

Hello, 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.

vlyalko’s picture

StatusFileSize
new776 bytes

Status: Needs review » Needs work

The last submitted patch, 29: drupal-3101714-28.patch, failed testing. View results

vlyalko’s picture

Version: 9.4.x-dev » 9.3.x-dev
StatusFileSize
new984 bytes

This patch will NOT truncate URL. If URL TITLE is NULL, it will use truncated URL as a TITLE.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

pameeela’s picture

I 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.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

borutpiletic’s picture

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.

angrytoast’s picture

Version: 9.5.x-dev » 11.x-dev

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:

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'trim_length' => '80',
      'url_only' => '',
      'url_plain' => '',
      'rel' => '',
      'target' => '',
    ] + parent::defaultSettings();
  }

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.

mahtab_alam’s picture

StatusFileSize
new1.14 KB

Prashant.c made their first commit to this issue’s fork.

prashant.c’s picture

Status: Needs work » Needs review

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.
Only local images are allowed.

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

Thanks

smustgrave’s picture

Status: Needs review » Needs work

This kind of change will need test coverage.

lmoeni made their first commit to this issue’s fork.

lmoeni’s picture

Status: Needs work » Needs review

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?

divya.sejekan’s picture

StatusFileSize
new73.66 KB
new135.96 KB
new197 KB

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

smustgrave’s picture

Category: Bug report » Feature request
Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

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.

longwave’s picture

Status: Reviewed & tested by the community » Needs work

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.

Keshav Patel made their first commit to this issue’s fork.

tobiasb’s picture

Status: Needs work » Needs review

The 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.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

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.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review

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.

smustgrave’s picture

StatusFileSize
new73.22 KB

So is the current proposal

That 80 is left as is

If you check the two boxes like so

example

Then the trim should disappear?

arun.k’s picture

Status: Needs review » Reviewed & tested by the community

@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.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

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).

mherchel’s picture

One is display the link in an anchor tag. Here the trim makes sense because you are not breaking the link.

I 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)

hosterholz made their first commit to this issue’s fork.

hosterholz’s picture

StatusFileSize
new8.26 KB

Since 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.

dcam’s picture

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

Honestly, 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.

tobiasb’s picture

We 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#com

This 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.

tobiasb’s picture

Status: Needs work » Needs review
smustgrave’s picture

@alexpott thoughts?

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

smustgrave’s picture

Status: Needs review » Needs work

Can this one get a rebase please.

tobiasb’s picture

Status: Needs work » Needs review

MR rebased.

fabrondeau’s picture

Hello,
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:

  • core/modules/link/tests/src/Kernel/LinkSeparateFormatterDisplayTest.php
  • core/modules/link/tests/src/Kernel/LinkFormatterDisplayTest.php
needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new548 bytes

The 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.

tobiasb’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Maybe this could be a change we just land in 12 if there's disruption concerns? But appears to be working as advertised.

quietone’s picture

Issue summary: View changes

I 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.

quietone’s picture

Title: Link field display defaults to trim at 80 chars, breaking longer plaintext URLs » Link field display should show plaintext URLs longer than 80 chars

  • catch committed 5dc59406 on 11.x
    feat: #3101714 Link field display should show plaintext URLs longer than...

  • catch committed 0c75f8ed on main
    feat: #3101714 Link field display should show plaintext URLs longer than...
catch’s picture

Version: main » 11.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/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.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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