Problem/Motivation
Link fields and menu links are output in JSON:API in the following format:
"field_link": [
{
"uri": "internal:/",
"title": "Home",
"options": [
]
},
{
"uri": "entity:node/5",
"title": "News Center",
"options": [
]
}
]
This isn't very useful for anything consuming the API because it needs to process these links. In most cases the data can't be fetched because it's unknown what node type is being referenced, and so which endpoint to query is unknown. If this was known, then it would require a lot of unnecessary requests to get the links from various bundle endpoints.
Proposed resolution
Add a computed resolvable_uri property at \Drupal\link\Plugin\Field\FieldType\LinkItem::propertyDefinitions()
"field_link": [
{
"uri": "internal:/",
"resolvable_uri": '/'
"title": "Home",
"options": [
]
},
{
"uri": "entity:node/5",
"resolvable_uri": '/node/5'
"title": "News Center",
"options": [
]
}
]
Also add the ability to set the field item's value by assigning the resolvable_uri property.
Remaining tasks
User interface changes
API changes
An additional property resolvable_uri is added to LinkItem.
The value of a Link field item may be set by assigning the value of the resolvable_uri property, for example:
$entity->field_link->resolvable_uri = 'https://www.drupal.org?test_param=test_value#main-content';
Data model changes
Adds a new computed field so no change in the data model.
Release notes snippet
A new property resolvable_uri has been added to the link field to access the generated URL.
Edit.: The new field was going to be called just url but it caused some twig override issues (see #22,#26,#27 so full_url was used instead as suggested in #67). Later, the property name was changed to resolvable_uri to be more descriptive.
| Comment | File | Size | Author |
|---|
Issue fork drupal-3066751
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
wim leersThanks for filing this issue! 🙏
Comment #3
justafishComment #5
wim leersFrom
MenuLinkContentHalJsonAnonTest:This means it's working! 🥳
If you update the existing tests' expectations, we'll have fewer failures.
Comment #6
justafishComment #7
justafishComment #8
jibranI do think adding a computed field to
LinkItemis not the right approach here.\Drupal\file\Plugin\Field\FieldType\FileUriItemis already doing that.This problem also exsits for
\Drupal\file\Plugin\Field\FieldType\FileUriItemand\Drupal\Core\Field\Plugin\Field\FieldType\UriItemboth have properties with\Drupal\Core\TypedData\Plugin\DataType\UriDataType.We should update
::getValuemethod of\Drupal\Core\TypedData\Plugin\DataType\UriDataType so that if parent has the external true option/setting set then it should provide an external URL.Comment #9
justafish@jibran I do see that that FileUriItem is doing something similar, but not sure how your solution solves this problem. Could you clarify please?
Comment #10
wim leers#7: yay, number of failures cut in half! 🥳
Like @justafish, I'm not following @jibran's comment.
Comment #11
justafishIf the suggestion was to remove/replace the uri completely with the computed one, I think that's still needed in order to facilitate Create/Update requests
Comment #12
jibranMy suggestion is to not add URL field at all and use uri property to provide URL when
\Drupal\Core\TypedData\Plugin\DataType\Uriis serialized.Something like this in
\Drupal\Core\TypedData\Plugin\DataType\UriWe can also remove URL field from
FileUriItemand implement__getand__setto porvide BC here.Comment #13
justafishComment #14
justafish@jibran the current format of the uri field e.g. "entity:node/5" is still required to perform Create/Update operations afaik
Comment #16
justafishOpened a new issue for the remaining failures https://www.drupal.org/project/drupal/issues/3067609
Comment #18
jibranUpdated the patch.
\Drupal\link\Plugin\DataType\LinkUrlComputed::setValue()method along with implmentingCacheableDependencyInterface.\Drupal\link\Plugin\Field\FieldType\LinkItem::onChange()to keep the field values in sync with computed property just ERItem.Comment #19
jibranReroll.
Comment #21
wim leersThis looks great! I only have nits:
Nit:
===Why sometimes
assertEqual(), sometimesassertEquals()? 🤔Comment #22
greg__ commentedSomehow I have a problem with this portion of code
It seems to override what twig do so in my twig template if I call a field.0.url.routename it returns null when before I had a route name like entity.node.canonical.
Changing it to
fixed the problem.
Sorry I have not much time to dig more
Comment #23
jibranSo
field.0.urlis calling\Drupal\link\Plugin\Field\FieldType\LinkItem::getUrl()?Comment #24
nils.destoop commentedJust tested this patch on my 8.8 setup. Works like a charm
Comment #25
wim leers#22: can you reproduce that problem with just Drupal core? Or do you have some custom Twig-enhancing modules installed? Because I don't see how
field.0.url.routenamecan do anything with just Drupal core?Comment #26
jibranYes, because
field.0.url.routenametranslates to$field->get(0)->getUrl()->getRouteName().Comment #27
lauriiiIndeed,
field.0.url.routenamecan translate to$field->get(0)->getUrl()->getRouteName(). This has been explained here: https://twig.symfony.com/doc/3.x/templates.html#variables. In the template the code could be changed to{{ field.0.getUrl.routename }}which should work with the patch.Comment #28
wim leers#27: But it sounds like you would consider this a BC break, right?
Comment #29
lauriiiI'm on the fence about this. Based on https://www.drupal.org/core/d8-frontend-bc-policy#render-arrays, I probably wouldn't consider this as a BC break, but I would still provide a change record to warn people about this change. I think it would be good to get an opinion from a release manager on this.
Comment #31
weri commentedThe patch #19 works as expected, but it would be nice, to have an option to get always absolute URLs instead of relative.
Comment #32
duozerskThis one is the same as 19, just removed the git binary part at the very beginning of the patch.
It was failing:
Comment #33
jonnyeom commentedHere is an updated patch that can be used in conjunction with the patch from issue #2915792: MenuLinkContentAccessControlHandler does not allow "view" access without admin permission, making these entities inaccessible via REST, JSON API and GraphQL and entity reference fields.
My composer.json has the patches in this order.
Comment #34
jibranRerolled #19 and addressed #21.
Comment #36
jibranThis will fix some fails.
Comment #38
jibranThis fix remaining fails.
Comment #39
acbramley commentedCode looks good to me, thorough tests and great commentary. Just up in the air if we need a test-only patch or not. Otherwise RTBC from me!
Comment #40
jibranI'll add my comment from slack here.
The one thing you can ask about it is missing test coverage of a new feature which I think is covered by this code block.
so IMO we are good here from testing POV.
Comment #41
acbramley commentedAs per #39, also reviewed by @Sam152
Comment #42
krzysztof domańskiRe-rolled
Comment #43
acbramley commented@Krzysztof Domański patch #38 still applies cleanly, no need to reroll.
Comment #44
justafishComment #45
alexpottThe change record doesn't really address #22/ #27 and there's not been the request release manager about the potential BC break. I think the fact that someone has managed to have this problem and report it on the issue before we've even committed it should give us concern.
Will this change break
{% set bundle_attributes = bundle.add_linkgetOption('attributes') ?: {} %}in core/themes/claro/templates/entity-add-list.html.twig?Looking at http://codcontrib.hank.vps-private.net/search?text=.url.&filename= we've got:
http://codcontrib.hank.vps-private.net/node/29411413
http://codcontrib.hank.vps-private.net/node/29424660
http://codcontrib.hank.vps-private.net/node/29427291
http://codcontrib.hank.vps-private.net/node/29454278
On the first page.
This looks disruptive in its current form. Perhaps we need another name? :(
Comment #46
jibranIt is disheartening to see #45 after a month long RTBC. 🤐
Comment #47
alexpott@jibran sorry about that. As I'm sure you're aware the rtbc queue has been very long for a while but we're finally getting in under control.
The comments referred to in #45 were posted more than two months before the issue was RTBCed, and were not addressed. When there are unaddressed comments on an issue (even if they were rejected for a good reason), and there is not a clear resolution, this requires a lot of additional time for committers to read through and figure out whether they were valid or not, which slows down progress in the RTBC queue overall. Anyone involved in the issue can ensure that all comments are addressed (and ideally mentioned in the issue summary if they're significant) whether the issue is RTBC or not
Comment #48
jibranThanks for the reply @alexpott. I really appreciate all the work you and the rest of committer team is doing to bring the RTBC queue down.
I'm going to put the issue in NR so that release managers can review.
Comment #49
alexpott@jibran looking at the number of affected templates I think we need another plan - or at least to try to come up with another option.
Comment #50
gatorjoe commentedPatch #38 appears to have solved the issue for me. Thank you!
Comment #52
jonnyeom commentedThis is a version of patch #38, applied on top of the patch for #2915792: MenuLinkContentAccessControlHandler does not allow "view" access without admin permission, making these entities inaccessible via REST, JSON API and GraphQL and entity reference fields
Comment #53
jonnyeom commentedFixed a test and added referenced patch number to patch file
Comment #54
Sebastien VR commentedReroll
Comment #55
Sebastien VR commentedIgnore above, forgot the LinkUrlComputed.php
Comment #58
kim.pepperComment #59
vsujeetkumar commentedRe-roll patch created for 9.3.x.
Comment #61
vsujeetkumar commentedFixed fail tests, Please have a look.
Comment #62
vsujeetkumar commentedFixed some cs issues.
Comment #63
vacilando commentedIs this is a duplicate of #2942851: Link enhancer to add Aliased URL for internal links?
Comment #64
kristen pol@Vacilando Fyi, I tested #2942851: Link enhancer to add Aliased URL for internal links and it didn't add
urlfor paragraphs (only nodes) but this core patch added it to paragraph fields.Example...
Before:
After:
Comment #66
jonnyeom commentedThis patch is working very well for me on 9.3.5.
Thanks!
Comment #67
jibranUnfortunately, this is not ready we need to address #22 as stated in #45.
How about we change url to full_url?
Comment #68
andregp commentedHere is a patch addressing #22 using #67 suggestion. I also fixed some CS on the patch code so it don't introduce new phpcs errors on the core.
Comment #69
jibranThanks for the patch. Let's update the change record and IS before we finish reviews and get this RTBC.
Removing "Needs release manager review" tag as we have addressed the points raised in #45.
Comment #70
andregp commentedComment #71
andregp commentedIS updated.
I also updated the change record, but didn't remove the related tag as I'm not sure the change record is 100% correct now. I'll wait for a second review on the CR (https://www.drupal.org/node/3143736).
Comment #72
kristen polI'm a bit confused about switching things to
full_urlwhich makes me think it's an absolute (with domain) link but it can be absolute or relative. What is the reasoning with calling itfull_urlinstead ofurl?Comment #73
andregp commented@Kristen Pol
The name change from
urltofull_urlwas to address the problem raised by #22and confirmed by #26 and #27
It doesn't need to be
full_urlbut has to be something different from justurl. I just used the name suggestion from @jibran on #67. :)Comment #75
niles38 commentedI tried the patch on #62 and #68 but it didn't work for me. We're running Drupal 9.3.13. I will have to admit I'm new to using the JSON API to retrieve data.
My array inside the
field_linkinside a paragraph is still the following (when I console.log):How are other people getting the value from field links? Do they work in fields on nodes and not on paragraphs?
Comment #77
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #79
nitesh sethia commentedMaking the patch compatible with latest Drupal 10.3 version.
Comment #81
smustgrave commentedHiding patches for clarity
MR should be pointed at 11.x as the current development branch
MR also appears to have some test failures
Previously tagged for CR updates which appear to be needed as well.
Thanks
Comment #82
acbramley commentedComment #83
dgroene commented#79 is not applying for me on 10.3.1NVM- #79 applies for me on 10.3.1 when I use depth 3Comment #84
dgroene commentedSame patch as #79 with depth 2.
Comment #85
dgroene commentedThis is a patch that is intended for those who need to support both "url" and "full_url" while decoupled front ends make this change.
Comment #90
dcam commentedI see from the comments that naming the property
full_urlwasn't necessarily a popular choice. I'm not fond of it either. It isn't descriptive or meaningful.I renamed the property from
full_urltoresolvable_uri. I wouldn't mindresolvable_url, but I decided to stay with "uri" to keep the association with the originaluriproperty. Feel free to provide feedback or other suggestions.I recreated the MR for Drupal 11. The original MR was hidden and a new one was created because usually changing the MR version breaks everything. Recreation is often the easiest way forward. I had to fix an issue with the JSON:API
ShortcutTest. There's a comment on the MR about it.The issue summary and change record have been updated.
Comment #91
dcam commentedComment #92
smustgrave commentedLeft a comment on the MR.
Comment #93
dcam commentedI responded to the feedback.
I also updated the issue summary with information about the ability to set the field item's value by assigning the
resolvable_uriproperty.Comment #95
dgroene commentedI appreciate the work everyone is doing on this, and I don't want to be a complainer- but the repeated name changes are excrutiating. When you have dozens of front end applications relying on "url" getting them to change to "full_url" and redeploy their applications was painful. Now we have everybody using "full_url" and we are moving once again to "resolvable_uri." This is not a trivial change if you have a large number of production apps already relying on full_url. I am not seeing why resolvable_uri is an improvement.
Comment #96
dgroene commentedPatch for those who need to support full_url and resolvable_uri for some time (P2).
Comment #97
smustgrave commentedBelieve feedback for this one has been addressed. Not sure about full_url vs resolvable_uri but do agree didn't seem like full_url was heavily agreed upon. See no issue with @dcam proposal
Comment #98
godotislateThe "Needs release manager review" tag was removed in #69, but I did not see a release manager sign off, so adding the tag back.