Problem/Motivation
In a Link field's settings configuration page, it is impossible to set a default value for the "Link text" subfield while leaving the URL subfield with no default.
One use case for doing this is to have preset link text for consistency while expecting the URL to change per entity. Consider the example of a website about books. Each book is a node with a link field. The default text of these links might be "Search for this book at nearby libraries" while the URL must be customized for each individual book. This would save editing time and give some guarantee of consistency across nodes.
Site builders requiring default link text functionality are advised to use the Fixed text link formatter module while this issue is in progress or as a permanent solution.
Steps to reproduce
- Add a link field to a content type.
- The link field is configured with "Allow link text" set to be required.
- Under Default Value, enter text for the "link text".
- Leave the "default URL" empty.
- Save the field configuration.
Edit the link field and see that the default link text has not been saved.
Proposed resolution
The problem here is that LinkItem::isEmpty() returns true if the item does not have an URI, then WidgetBase::extractFormValues() calls filterEmptyItems() and that removes the items that only have a text but not an URI.
This behavior is wanted when a user is filling the form normally but not when setting the default value.
However, we lack the context in LinkItem::isEmpty() to know if we are in the default value form or not.
The proposed solution involves overriding extractFormValues() in LinkWidget and not to call filterEmptyItems() if we are in the default value form. This allow settings the default value in the config but then this value is still not used in the real form.
Remaining tasks
Usability review, focusing on the fact that now a field not marked required is required.
Review
Commit
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #157 | 2708101-157.patch | 6.77 KB | michaelsoetaert |
Issue fork drupal-2708101
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
Comment #2
clairedesbois@gmail.comI confirm the problem.
If I add a default URL and a default link text, values are save.
If I add only a default URL without default link text, it works too.
But If I add only a default link test without the URL, the default link text isn't save.
Comment #3
pifagor commentedComment #4
pifagor commentedComment #5
pifagor commentedPatch
Comment #6
pifagor commentedOr
Comment #7
pifagor commentedComment #8
pifagor commentedComment #11
pifagor commentedComment #12
andypostmain property of the field is *uri* so title should be validated additionally
Comment #13
pifagor commentedAdd patch for validate
Comment #14
pifagor commentedComment #15
pifagor commentedComment #16
alexpott@pifagor there's no need to contact me through my d.o contact form to ask for patch review. As this is fixing a bug it needs tests.
Comment #17
pifagor commentedComment #18
pifagor commentedComment #19
pifagor commentedComment #20
amit.drupal commented#13 patch is not working.
Comment #21
amit.drupal commentedAdd patch for validate.
Comment #23
pifagor commented@amit.drupal
#13 patch is not working.
Please argue.
After https://www.drupal.org/files/issues/Screenshot%20from%202016-05-23%2017_... a screenshot from my patch. And it does not keep the title, without url. What is logical and correct.
Comment #24
pifagor commentedComment #25
sushantpaste#13 is not working.
@pifagor I think that patch will only work for you as may be your link field name is "field_link", it will not work for others as they can have different field name.
You need to change below line in your patch
with
hope this helps.
Comment #26
sushantpasteComment #27
pifagor commented@sushantpaste thank you for your comments
URL field is required for the title. For functional module link title field will not be saved, because is a must. Your code is valid only for a clear field. If the field is meaningful, your code is incorrect.
Fixed patch it should work
Comment #28
pifagor commentedAdded screenshots, evidence that the patch works
Comment #29
pifagor commentedComment #30
pifagor commentedComment #31
pifagor commentedComment #32
pifagor commentedComment #33
amit.drupal commented#27 Is Working Fine for me .
Comment #34
sushantpaste#27 Works fine. Marking it as RTBC.
Comment #35
pifagor commentedComment #36
eric_a commented@pifagor, @sushantpaste, the tag "Needs tests" refers to tests in code. For manual testing we have "Needs manual testing". So somebody needs to add a test to the patch and then both the test and the plain fix must be reviewed.
Comment #37
pifagor commentedComment #38
mac_weber commented@pifagor please read #36. Change the issue issue status only after adding tests in code.
Comment #39
minnoce commentedComment #40
mac_weber commented@minnoce please keep issues on development version and ask before doing changes if you are not sure.
Read the guidelines at https://www.drupal.org/node/314328#version
Comment #41
mac_weber commentedComment #42
minnoce commentedSorry, I haven't read carefully the guidelines.
Clearly, I noticed this bug in 8.1.3 (vanilla freshly installed on a Lamp).
Thanks
Comment #43
Ilesyt commentedHello, I am creating a website where I put in a link field in a content type.
I want authors to be able to put in a link but not add link text as it should be a default text (such as "Check it out").
The links are outgoing.
So when working with the link field, I stumbled upon this issue.
If I disable the link text (because I don't want others messing with it) and still input a value (on field creation), it will disappear.
If I enable link text (although I do not want to), if I don't put in an URL value, the link text disappears.
Only if I put in a default URL value, does the default link text stay.
I would like an option where could I input a link text in the 'Manage display' when editing the field display.
Comment #47
pifagor commentedComment #48
dani3lr0se commentedI'm sorry, but I'm a little confused. Is this patch supposed to allow adding default link text without a URL? If so, it's not working for me. I tested #27 on 8.5.x and it still requires a URL if I want default link text. See attached screenshot. I mean, I understand that it won't work unless there is a URL because why would we want link text to display without a URL? But, if this makes it so the link text only displays if there is a URL, then that's understandable. Sorry if I sound snarky or anything. I don't mean to be. I think this is an interesting issue, especially for use cases such as #43 above. Thanks for working on this. :)
Comment #49
pifagor commentedThe link cannot be without a URL. Can be a link without text.
See please - https://www.drupal.org/project/drupal/issues/2708101#comment-11233567
Comment #50
dani3lr0se commentedThis is still a little confusing. The issue summary describes not being able to save default link text without a URL. The comment #27 is stating that the URL is required for the title. Is the patch addressing the issue or should the issue summary be updated?
Comment #52
mangy.fox commented@daniel_rose I agree that this issue has become confused, because the given patch does not address the original issue.
I also do not think that the original issue can be easily resolved. When you set the default value of a field, you are setting the default value for the entire field. As such, that default value needs to be a valid field value. As @pifagor points out, a link without a URL is not valid, and so it makes sense that we should not be able to set a default value that does not contain one.
I propose that we change the issue to something like "Default value for link text does not trigger validation".
Comment #53
michelleI just ran into this problem. I am trying to set the default value of the text and also the check box "Link to this page (Leave URL blank)". I'm not 100% sure that checkbox is part of core as I haven't seen it before and I'm new to this project. Even so, it is a use case for setting the link text without a URL so this issue is needed.
Comment #55
dani3lr0se commentedJust wanted to make a note here that I've run into this issue on a project. Client would like to have default link text on a link field, but it doesn't save when I add it. This is definitely a use case and I agree with others that it's a necessary issue still.
Comment #57
asak commentedSame here - The client is requesting to have a link field, with a required title, with a default title set and an empty URL field, with the option to override the title when saving the entity. This is a legitimate request that matches the original description of the issue, but the patch and approach above do not address this at all.
The suggested module (https://www.drupal.org/project/fixed_text_link_formatter) also doesn't solve the issue, as it uses a fixed title for all links in all entities instead of a custom title per entity (if so desired).
Marking as "needs work" and I'll give this some more thought.
Comment #58
volkswagenchickTagging for DrupalNorth 2019
Comment #59
yago elias commentedHi! Have you noticed this problem with a specific role? I am having this problem when it come to a editor role, but if I try the same scenario with administrator I don't have this problem
Comment #60
volkswagenchickTagging for DrupalCamp Colorado Contrib day (Sunday August 4)
Comment #64
tarik.cipix commentedThis problem still persists in 8.9.13, any status updates?
Comment #65
martijn de witGuess the patch from #27 needs a reroll, and there are still tests missing according the issue tags.
Comment #66
tarik.cipix commentedLike mentioned in #57 (https://www.drupal.org/project/drupal/issues/2708101#comment-13100404)
The patch in #27 does not really address the issue layed out, it just shows a message that reminds admins about this issue.
Comment #67
martijn de witmy bad.
patch #27 only inlcudes:
That is no solution to the real problem indeed.
Comment #68
prudloff commentedThe problem here is that
LinkItem::isEmpty()returns true if the item does not have an URI, thenWidgetBase::extractFormValues()callsfilterEmptyItems()and that removes the items that only have a text but not an URI.This behaviour is wanted when a user is filling the form normally but not when setting the default value.
However, we lack the context in
LinkItem::isEmpty()to know if we are in the default value form or not.I tried duplicating
extractFormValues()inLinkWidgetand not callfilterEmptyItems()if we are in the default value form. This allow settings the default value in the config but then this value is still not used in the real form.Comment #69
martijn de witComment #70
tarik.cipix commentedThank you for picking up this thread, I have tested your patch #68.
This does allow admins to save a default value in the link field config, but when adding a new node, the value is not pre-filled.
Tested on Drupal 8.9.13.
Comment #71
heikkiy commentedTested also patch from #68 and verified that it does allow saving the value now but the value is not filled either in content add or edit page.
Tested on Drupal core 8.9.13.
Comment #72
vakulrai commentedI have worked on the solution given by #68, as it is actually saving the default value for "title" field but the same is not coming up on the node form.This patch will take care of the value on node form.
I have also tweaked the unlimited value type , which was also behaving the same manner.
Added a test for the same.
Kindly review.
Comment #73
vakulrai commentedFixing the CSpell issue in patch #72.
Comment #75
tarik.cipix commented#73 applies on 8.9.14 and seems to work great. Thank you for this patch.
Comment #76
martijn de witSeems there are still some test failures.
Patch is for 9.2.x-dev and an user test for 8.9.14.
This is not enough to change status.
Comment #78
neslee canil pinto$url variable seems to be not accessible here
added a patch to pass the tests.
Comment #79
chetanbharambe commentedVerified and tested patch #78.
Patch applied successfully but functionality not working
Testing Steps:
# Goto: admin/structure/types/manage/article/fields
# Click on Add field
# Select a Link from "Add a new field"
# Mention the Label name
# Click on save
# Go: node/add/article
# Leave the URL field is empty as it is not required
# Click on Save
# User is not able to Save and able to see the error "The URL field is required when the Link text field is specified."
Expected Results:
# User should be able to Save the node without mentioning the URL field (as URL field is non required)
Actual Results:
# User is not able to Save and able to see the error "The URL field is required when the Link text field is specified."
Not working as expected.
Moving to Needs Work.
Comment #80
chetanbharambe commentedComment #82
bnjmnmThe review in #79. is based on an incorrect understanding of the requirements.
That is incorrect. It's actually a good thing that this is happening. This issue is to allow a default link text value without including a default URL value for a field. When the field is in use, it should still enforce the presence of a URL value if the link text is not empty.
Comment #84
bnjmnmReview on the Merge Request that is mainly nits.
Just to see if there was a way to avoid duplicating code, I tried to see if there was a way to not have to override
extractFormValues()so fully. I didn't find it, and with a little bit of changes to the docs the duplication shouldn't be that big a deal. Hopefully the test coverage will spot any issues that may arise if the parentextractFormValues()is modified andLinkWidgetis out of sync with it. Maybe another reviewer will find a slick way around it, but the benefits seem to outweigh the drawbacks of a little duplication.I manually tested and adding a default value for Link text without URL works great, and when the field is in use, URL is still required if Link Text has a value.
Comment #85
shivansunfire commentedI've addressed all the suggestions in the merge request https://git.drupalcode.org/project/drupal/-/merge_requests/877, but I haven't been able to push the code back, I receive the message "You are not allowed to push code to this project", despite I clicked the "Get push access" button here in the issue, and it says now "✓ You have push access".
So I'm attaching the patch with the changes.
Comment #87
shivansunfire commentedReverted line 239 of LinkWidget.php to make the tests pass. Attaching new patch.
Comment #88
omrmankar#79 as comment please review the patch.
Comment #89
ranjith_kumar_k_u commentedRe-rolled #88
Comment #91
shivansunfire commented@omrmankar I don't understand why have you uploaded a patch when I already addressed the changes and suggestions from patch 79's review in merge request. That patch is not taking into account all the feedback from the merge request, as I did. And it's not applying.
So @ranjith_kumar_k_u we shouldn't re-roll patch 88, which is failing again.
The issue was in needs review from patch 86, it's the patch that works and addresses all changes suggested by @bnjmnm and @douggreen review. Hidding the last two attempts and putting this back in needs review.
Comment #92
shivansunfire commentedComment #93
vikashsoni commentedapplied #87 patch after patch we can set Default Value in text field but we can not save node
for reference sharing screenshot ..
Comment #94
chetanbharambe commentedVerified and tested patch #87.
Patch applied successfully and looks good to me
Testing Steps:
# Goto: admin/structure/types/manage/article/fields
# Click on Add field
# Select a Link from "Add a new field"
# Mention the Label name
# Click on save
# Go: node/add/article
# Leave the URL field is empty as it is not required
# Click on Save
# User can set Default Value in a text field but the user can not save the node
Working as expected.
Please refer attached screenshot
Can be a move to +1RTBC
Comment #95
paulocsI'll organize this issue to keep the MR approach as @juanolalla was not able to push his changes.
@juanolalla maybe your ssh key is not configured in drupal.org. See: https://www.drupal.org/drupalorg/docs/user-accounts/git-authentication-for-drupalorg-projects#s-authenticating-with-ssh-keys
Comment #96
paulocsI update the MR based on the patch #87. I also merged
9.3.xinto2708101-default-value-forbranch.The patches #88 and #89 are wrong so I didn't consider them when I updated the MR.
Comment #97
paulocsTagging the Bug Smash Initiative.
Comment #98
shivansunfire commentedI could solve the issue with the default value overriding the current value for that field in the merge request. Uploading here also an updated patch.
Comment #99
volkswagenchickAdding tag for DrupalCon
Europe2021. ThanksComment #100
douggreen commentedI still think for core that the duplication is unacceptable, for contrib maybe ok, but not for core. This is only a minor problem for my client, so I'm not going to be able to put more time into this. But I think we need a totally different solution. Would extending FieldItemList to LinkFieldItemList give us a place to do this?
In LinkTypeConstraintValidator::validate() $url will always be set, this change is unnecessary, tests pass without it.
Comment #101
roshanibhangale commentedVerified and tested patch #98.
Patch applied successfully but functionality not working
Testing Steps:
Expected Result
Actual Result
Not Working as expected.
Please refer attached screenshot.
Hence moving to Needs Work
Comment #102
roshanibhangale commentedComment #103
roshanibhangale commentedComment #104
shivansunfire commented@RoshaniBhangale Thank you so much for your work and your effort!
However, what you assumed as expected result, is not correct. See explanation in #82. When the field is in use in a node, it should still enforce the presence of a URL value if the link text is not empty. So this is working as expected.
Comment #105
thatguy commentedPatch at #98 applies correctly for Drupal 9.2.7. Fixes the issue of setting default value for link text without setting the default url.
Comment #106
quietone commentedNice to see an older issue getting near completion. Thanks everyone.
I came to review but the Issue Summary is incomplete. There are steps to reproduce which is great but there is no proposed resolution or remaining tasks. Tagging for update.
There is also an MR and a patch here - which one is to be reviewed?
@RoshaniBhangale, thanks for the screenshots. They need to be places in the Issue Summary where it is easy for reviewers and committers to find them. I also think it helps to add a comment in the remaining tasks that you tested and the patch works or does not with a link to your comment for more details.
Comment #107
shivansunfire commentedComment #108
shivansunfire commented> There is also an MR and a patch here - which one is to be reviewed?
Both are up to date.
This has already been reviewed and approved by the community in 105#.
Comment #109
shivansunfire commentedComment #112
shivansunfire commentedThe merge request needed to resolve merge conflicts. I've resolved that and updating here the patch.
Comment #113
shivansunfire commentedComment #114
paulocs@juanolalla i know you are putting a big effort in this issue and thanks for that, but you don't need to upload the patch of the MR all the time.
The issue page and the gitlab page provides a link to download the patch of the MR.
Next to the issue fork information you have an option "plain diff" where you can download it. :)
Comment #115
shivansunfire commentedThanks @paulocs, I already know that, but I'm am concerned that if I used that merge request diff patch and someone changed it in a way that something wouldn't work as expected, my site could be broken. That's why I'm also uploading the patch here, so I can use that specific patch and have that control. I don't know the way to use a partial patch from a merge request at a specific point, if there's any.
Comment #116
quietone commented@juanolalla, I think good practice is to have the patch local and use https://github.com/cweagans/composer-patches.
I tested on Drupal 9.4.x, standard install.
Without the patch I confirmed that the default text is not saved. What makes that a problem is that the user is not informed.
With the patch the problem in the IS is resolved. When I then went to add a node and did not enter a link url, I got an error message. "The URL field is required when the Link text field is specified." That makes sense, given this change but it makes we wonder if this change should be made. At least, for me, It seems unusual to have default text for different URLs.
This issue has not had a usability review, so adding tag. The usability team prefers the IS to be up to date and to have screen shots in the IS. It would also help to include reasons why this change is needed in the IS. Setting to NW for that.
Removing credit for unnecessary screenshots.
Comment #118
murzMy use case with default text for different URLs is the "Download" button with link to a file on CDN, so the content manager should have the "Download" button text by default and type different URLs, but also should have ability to sometimes change it to custom text like "Click here to get the file".
So I was pretty surprised when was trying to save the default "Link text" with the empty URL on default Drupal install, because it always disappears after save. And this patch works pretty well to solve that simple misapprehension!
Comment #120
renukakulkarni commentedRe-rolled the patch to work with 9.5.x.
Comment #121
csakiistvan@renukakulkarni for me this patch is is very nice, but the link field will mandatory on the form display (but my field is not mandatory on the field settings.)
Comment #123
emerham commentedAlso using field tokens you can have a mostly static title that replaces part of the text on render.
For instance we have some PAC classes and we want the link to be:
View the OSU course catalog page for [node:field_pac_class_name].As long as you make the token field required then it should work. Tested on a site that I'm migrating and it works with this patch.
Comment #124
damienmckennaFYI there's a related feature request for allowing a static link title: #1829468: Link field: add the ability to have a static anchor text and link title
Comment #125
damienmckennaThe patch mostly works. However, if a default title is provided, when trying to save the entity form the URL field will be required, there's no way of saving the field without an URL. Which is what @csakiistvan said in #121.
Comment #126
pguillard commentedNice patch, I agree with #125.
I guess it's worth a Needs review, if not RTBC yet.
Comment #127
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 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 #128
douggreen commentedI rebase and force pushed the branch. It needs review by someone who is familiar with what this does.
Comment #130
emerham commentedI rerolled the patch from 120 that was working
Comment #132
shalini_jha commentedI have replicate this issues and after applying the changes in the MR 8552, the link text default value save is working properly.
I have observed the pipeline failure, and i have fixed the pipeline failure and now all the test passes in MR 8552 against 11.x . same branch have MR 877 against 9.x , need to hide this MR as we are using MR 8552.
Apart from that i am also agree with #125 . but according to #126 i have fixed the pipeline failure and moving for NR.
Kindly Review.
Comment #134
smustgrave commentedIssue summary still has a TBD section.
Comment #135
shivansunfire commentedFrom #125:
From #121:
I see there is still some confusion with the natural core's behavior of the form display, forcing an URL to be set if there is a link text set, which is correct, and the issue we are fixing here, just allowing a default link text without a default URL in the field settings form.
We had that explained in #82:
So please, let's not bring again the form display behavior that is in core, which is correct, and it is not part of the issue we are dealing with here.
Comment #136
shivansunfire commentedThere are no interface changes with this issue, it's just a bug: the link text was not saved, and now with the fix it is saved.
So this doesn't need usability review.
No remaining tasks and nothing else, so moving this to RTBC.
Comment #137
shivansunfire commentedComment #138
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 #140
mrinalini9 commentedFixed phpstan issues reported by the bot and have updated the MR, please review it.
Thanks!
Comment #141
sagarmohite0031 commentedHello,
Verified and tested on Drupal 11.
The MR is applied successfully.
1.Add a link field to a content type.
2.The link field is configured with "Allow link text" set to be required.
3.Under Default Value, enter text for the "Test data for link".
4.Leave the "default URL" empty.
5.Save the field configuration.
6.Edit the link field and see that the default link text should saved.
Working as expected -
Default link text is saved successfully.
Check attachments-
Comment #142
shivansunfire commentedComment #143
quietone commentedWith the settings shown in the screenshot, before this change the link field is not required and after this change it is, even though the 'Required field' setting is not selected. I think that will cause confusion for admins and content editors, as it has done for people working on this issue. I think we find a way to resolve that here. For that getting an Usability review will help, so I am tagging and changing status.
Comment #144
shivansunfire commentedThat is not true, please see #135 for the explanation. This fix is not changing at all the current's Drupal core behavior, that requires an URL if you would like to set a link just with a link text.
This is just a simple fix for the existing bug on the field settings form: the default link text is not being saved.
This fix has nothing to do and touches nothing of the display form, there is no UI changes at all. Please open a new ticket to discuss current Drupal's behavior from an usability point of view, and let this fix solving the bug be committed.
Comment #145
shivansunfire commentedComment #146
quietone commentedYes, I read #135 and the other comments as well. Yes, this is a bug with a straightforward fix. That fix, however, has caused unexpected behavior as noted in #79, #101, #116, #121, #125 and #143.
And this fix does cause changes the UI, on a node/add form. The URL part of link field will be shown to the user as required. That is the change and yes it make sense. However, there is no information provided to let the use that the form can be submitted without completing the required field. That is why I asked for a usability review here 3 years ago and I still think one should be done.
Comment #147
amateescu commentedI think it would be easier to introduce a field item list class for the
linkfield type and override\Drupal\Core\Field\FieldItemList::defaultValuesFormSubmit()to manually return a default value if thetitlesetting is set to required and there's a value for the link title.Comment #148
shivansunfire commentedNo, this fix doesn't cause changes in the UI. What you are referring is not part of this fix, is Drupal core current's confusing UI.
Right now, if you want to save a default value for the link text, that's broken. This patch is allowing many sites to fix that, for years. It should be committed.
This is the current core behavior, not the result of this fix. If you try to save a link field with text and no URL. Please open a new ticket to improve that.
Comment #149
shivansunfire commentedI don't understand how that would solve the current problem of the UI's bug, failing to save the default value in the field settings form. And why are you changing the status of this bug fix to "needs work" again.
Comment #150
alexpottI'm not sure about the fix here. I think saving an invalid default value might result in more problems as all other default values are valid and not empty. I wonder if we should consider a different fix where we save the default title value somewhere else in the field config if the link is not present. That way we wouldn't have to override extractFormValues which feels a bit risky.
Comment #151
alexpott@juanolalla this does result in a behaviour change and it will be confusing for users because once you have a default link title it's completely not obvious that you could remove the link title to save the content without a link. This suggests a potential fix - only allow setting a default link title if the field is required.
Comment #152
alexpottOn posting the comment I do think we could consider a completely different fix. We could only allow a default link title to be set if the default link is also set. I've read the issue summary and I've not seen a use-case for providing a default link title without the link url. The link title and URL have to be related right - so the user needs to make the title fit the link.
Also as far as I can see the use of titles on links is largely discouraged - see https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/titl..., https://www.tpgi.com/using-the-html-title-attribute-updated/ and https://www.24a11y.com/2017/the-trials-and-tribulations-of-the-title-att... I don't think providing a default title with no related link is going to make this situation better.
Comment #153
shivansunfire commented@alexpott This issue is about the default link text, not about link title attribute.
@alexpott We are using this patch in many sites. The client needs to set a default link text so it is always the same text and the URL is changing: let's say for example "See the full source for more details", where the link pointing to the resource is different in every node.
The way I see this, more than "this does result in a behavior change", is that this actually allows something expected, the ability to save a default link text (instead of the frustration of doing it and see that it is not saved), and as you say, allowing the expected functionality allows the situation of a new node with a link text already filled and the user could be kind of forced to fill either the URL or delete the link text for that specific node.
However having to delete the link text to leave it empty so it passes validation without a URL in a non required field scenario, is something core is doing right now, not this fix. This fix is just removing a veil over an existing problem in core, hidden by the bug of the default text not actually being saved, despite the UI's supposedly allows it as with any other field. That's why I am encouraging to open a new issue about this, tackling what core does in the form display with links and URLs not filled on non required fields.
While it could be the most common scenario, we would not be satisfying all possible desired functionalities. I, as client, could perfectly desire to have a default link text for this field in all nodes that have an URL, which maybe is the 80% of the nodes for that content type, but not 100% so I don't want it to be required, but the default link text is a must, for consistency and time saving.
The real problem here is that, right now, core forces you to remove the link text if you write something on it and click save without filling the URL. This is the issue we are talking about now, the one unveiled by fixing this bug, +8 years open issue.
A possible solution to current's core not very good behavior, could be maybe to let the user have clear that the link text is ultimately dependent on the URL on a specific node edit/create form. So for example, if the link text input would be hidden and only be shown when the URL is actually filled, that would make things clearer, and possibly we wouldn't have to make the user delete the link text input if she/he finally saves the node without a value for the URL, as core currently enforces. As @quitetone says, that would require an usability look.
My proposal is to open a new issue from here to improve current's core confusing behavior, instead of leaving this bug unfixed forever so we not have to look at what is actually an incomplete implementation. If we decided not to fix this bug until we have a better display form implementation we should at least alert the user than if she/he takes the time to fill a default text link value, she/he should also fill an URL default value, or directly remove the option of filling a default text link. But I would love not to do these things, so many people as myself could at least be patching (and maintaining, rerolling) this to allow this necessary feature to our clientes. Many people are actually needing to have this failing feature solved.
Comment #154
dcam commentedI've been following this issue for years, since long before I became the Link module maintainer. I've also wanted to set a default text value for links while letting the URLs be customized per node. Now that I think about it, I still manage that application where I wanted that ability.
That said, I understand the potential usability issues at play here. I started thinking about other possible solutions and came to a realization that ironically was suggested back in 2016 in comment #43 by @Ilesyt:
The need that this issue is looking to solve would be really easy implement with a field formatter that has configurable default text. And wouldn't you know it, there's a module that does it: Fixed text link formatter. So if you need default link text functionality, then I advise looking into it.
Comment #156
michaelsoetaertCreated a patch based on the current state of MR 8552, which applies on
11.3.x.Comment #157
michaelsoetaertThe previous patch for
11.3.xdid apply (wrongly) on11.4.x, but caused a crash due toisNoLinkRoute-function already being part ofdrupal/core:Fatal error: Cannot redeclare Drupal\link\Plugin\Field\FieldWidget\LinkWidget::isNoLinkRoute() in /var/www/html/docroot/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php on line 595I've updated the patch to not add the
isNoLinkRoute-function anymore.Comment #158
shivansunfire commentedThe need that this issue is looking to solve is not that content appears with a fixed link not decided by editors but in configuration.
The need this issue is looking to solve is to allow editors to create links having already a suggested default configuration value in the node add form, a value they can use or change there. That module doesn't help with this at all.
Comment #159
shivansunfire commentedHere is a module to provide this missing functionality: Default Link Text Widget
Since this is 10 years old and we've been most of the time supporting a patch, I decided to contribute a simple module extending Link widget to allow to set this default value for link text in the widget through Form Display, so we don't depend on core solving this bug with all its implications.