Problem/Motivation
Currently if we want to get a Link object from a LinkItem object (i.e. field item of the "link" type), we have to get the Url and Title properties of the LinkItem separately and create instantiate a Link object ourselves:
$title = $link_item->get('title');
$url = $link_item->getUrl();
$link = \Drupal::Link->fromTextAndUrl($title, $url);
Steps to reproduce
Try to get a Link object from a LinkItem object
Proposed resolution
Make it possible to get the Link object with a single method call:
$link = $link_item->toLink()
Remaining tasks
None
User interface changes
None
API changes
A new toLink() method is added to Drupal\link\LinkItemInterface and Drupal\link\Plugin\Field\FieldType\LinkItem. It returns a Drupal\Core\Link object.
Data model changes
None
Release notes snippet
It is now possible to get a Link object from the LinkItem object.
Changed record drafted here: https://www.drupal.org/node/3458241
| Comment | File | Size | Author |
|---|
Issue fork drupal-2752187
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
thlor commentedComment #3
thlor commentedComment #4
dawehnerI really like this feature. I'm just a bit worries about the potential BC break, mhhhh
Comment #5
thlor commentedCan you elaborate on your worry about potential BC break? Since this feature doesn't remove or modify anything preexisting, my understanding is that it does not break BC. https://www.drupal.org/core/d8-allowed-changes#bc-break
Comment #7
thlor commentedSeems to me the test failure is caused by the testing automation's error (https://dispatcher.drupalci.org/job/default/163826/ "FATAL: Unable to delete script file /tmp/hudson5317119343148217404.sh").
Comment #8
dawehnerYeah that failure was a purely random one. Now that I look at this patch again we should ideally add some test coverage for it.
\Drupal\Tests\link\Kernel\LinkItemTestwould be a perfect place for it. Sorry :)Comment #9
pashupathi nath gajawada commentedHi Daniel Wehner,
As suggested in #8,
I have added the
Drupal\Tests\link\Kernel\LinkItemTest.Please find the updated patch.
Thanks,
Comment #10
thlor commentedRemoving "Needs tests" tag; test is supplied in #2752187-9: Add \Drupal\link\LinkItemInterface::toLink method.
Comment #11
bojanz commentedThe idea makes sense.
Question: would toLink() make more sense than getLink()? getLink() makes me think it's giving me a property, but it's actually constructing a link object with the data contained by the field.
Comment #12
dawehnertoLink sounds better indeed.
Comment #13
bojanz commentedLet's do the rename then, and add the test coverage. #9 did not provide it.
It would also make sense to optionally allow setting a Link object as the LinkItem value. We have a value object, let's use it fully.
Comment #14
sharique commentedAdded test for getLink method.
Comment #17
michaellenahan commentedHi there, this is basically a reroll of Sharique's #14.
Changed getLink() to toLink() as suggested in #11 and #12.
I notice that Entity has a toLink() as well, so that's better for consistency.
I think the test will fail, it did when I ran this locally.
I think that line 179 in LinkItemTest.php
$link = $entity->toLink();
... is referencing Entity toLink(); not LinkItem toLink();
Comment #18
thlor commentedComment #20
michaellenahan commented@thlor thank you for setting that last patch to "needs review".
I think I have this working now ...
Comment #21
michaellenahan commentedComment #22
riddhi.addweb commentedComment #23
tedbow@Jigar.tanna there is no need to tag issues with "drupal core", I have seen you add this tag to a few issue today. Drupal.org issue are in projects. Since this one is in "Drupal core (3060)" that is what determines it deals with "drupal core".
By that way would be interested in your help on Outside In issues if you have the time and desire.
Comment #24
dawehnerNitpick which can be fixed on the commit: The order for these for these parameters are: $expected_value, $actual_value
This gives you some warm and readable output when this test might ever break.
Comment #25
michaellenahan commentedThanks, Daniel. New patch attached.
Although, I notice that assertTrue() actually only has two parameters ... at least from what I see here in phpstorm ...
https://www.drupal.org/files/issues/2752187-1.png
https://www.drupal.org/files/issues/2752187-2.png
Comment #26
dawehnerYou are right about the message not existing in many phpunit assertions. These kind of messages don't add that many value, IMHO anyway.Comment #27
dawehnerThis should use assertEquals instead :) There 3 parameters make more sense
Comment #28
michaellenahan commentedYes :). While I was at it, I removed the other deprecated methods in the test.
Comment #29
michaellenahan commentedComment #30
michaellenahan commentedHmm. I guess I shouldn't do that. Removing those changes, because they are not relevant to the current issue.
Comment #31
dawehnerExactly :) Please open up a new issue for that maybe. To be honest though it would be great to do those deprecation replacements in one big go. So one method but over the entirety of the codebase maybe.
Comment #36
pasan.gamage commentedTested the patch #30 on 8.7 branch.
Tests passed.
Comment #37
larowlanThis is a great addition, I could literally use this and the chained ::toRenderable on at least 5 client projects tomorrow!
But we need to advertise its availability with a change record.
Can someone create one heralding the new feature with much fanfare.
Thanks
Lee
Comment #41
jennypanighetti commentedWhat is the current status of this? Should I attempt to re-roll the patch with 8.9.x? I really need this and thought I was going crazy that I have a LinkItem and I couldn't find a way to get an actual, renderable link from it.
Comment #47
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge require as a guide.
Believe it or not it appears patch #30 still applies to 10.1.x cleanly. Just triggered the tests for this.
From reading the comments sounds like this was all but done but never got a change record so sending to NW for that.
Comment #48
smustgrave commentedMoving to NW for the change record please. Can mark this once that happens.
Comment #49
bircherOmg! I follow this issue since probably six years.
I just went ahead and created the change record.
I checked the code too and it also looks RTBC to me, so setting the state to that.
Comment #50
xjmThanks for proposing this addition!
As mentioned in #3306201: Add a title getter to LinkItemInterface, adding new methods to an interface is a BC break. However, since the interface has a 1:1 relationship with the
LinkItemclass, method additions are allowed in minor releases. The change record should mention this BC break. (See my addition to https://www.drupal.org/node/3330231 for comparison.)Since this is a new method we're adding, it should have a return typehint. Reference:
https://www.drupal.org/docs/develop/standards/php/php-coding-standards#s...
toLink()should have params in docs.We can drop the assertion messages from these three assertions; core best practice has moved on to omitting them since this issue was first filed. :)
As with the other issue, we can also file a followup issue for the
linked_entity_referencemodule to implement this method as well.Comment #53
scott_euser commentedCreated a new change record here (since I don't have access to edit original): https://www.drupal.org/node/3458241
On the feedback in #50:
Comment #55
scott_euser commentedHide patches to avoid confusion with MR (and since this type of action tends to get ignored, anyone coming across this, please do not add a new patch, see this doc page for instructions).
Comment #56
smustgrave commentedHaven't reviewed yet but could issue summary be updated to the standard issue template please
Thank you!
Comment #57
scott_euser commentedUpdated issue summary
Comment #59
shalini_jha commentedI have checked this issue and issue summary. By calling the newly added toLink() method, I was able to get the link object as expected. I also verified the added test coverage and did not find any additional tags for this issue. I have rebased the MR since it was significantly behind, and the pipeline is now green. Moving this MR for review. Kindly review.
Comment #60
smustgrave commentedCR looks like it needs slight tweaking.
Versions seems off
Maybe a before of how users are retrieving this info currently
with an after of how this is easier.
Comment #61
shalini_jha commentedi am working for CR Update
Comment #62
shalini_jha commentedUpdated the CR according to the feedback based on the issue summary also. Moving this for NR.
let me know if anything else need to update.
Comment #63
nikolay shapovalov commentedMR looks good. But should we add test coverage new method at Drupal\Tests\link\Unit\LinkFormatterTest?
Comment #64
oily commentedRE: #63 I am starting on that. Used existing Unit test class for DRY reasons as so much setup for each test method. Have added use statement for Link class, Created a mock for it and set an expects() for the new toLink method.
Comment #65
nikolay shapovalov commentedThanks, RTBC.
Comment #67
quietone commentedI read the IS, comments, the MR and the change record.
I see this is tagged 'Needs change record updates'. That should be resolved before setting an issue to RTBC, thanks.
Continuing to triage, I changed the order of a 'use' statement in the MR. I re-arranged the CR so the most important part, the way things work with this change, is first. I then adding a remaining task to the issue summary. That is an update to the change records, to the tag can remain. I didn't find any unanswered questions. Oh, and I updated credit.
Comment #68
scott_euser commentedThanks!
Comment #69
smustgrave commentedCR looks fine to me.
I did rebase the MR as it was 600+ commits back, after a few re-tries it's still green.
Believe all feedback has been addressed for this one.
Comment #70
quietone commentedComment #71
xjmAmending attribution.
Comment #72
larowlanLeft some comments on the MR - I think we need to be defensive here for the edge cases like when
getUrlthrows an exception or the field-item is empty.Comment #74
dcam commentedI addressed the feedback.
Comment #75
oily commentedRan the test-only test:
I think this indicates the Unit test is working?
Comment #76
smustgrave commentedSeems feedback has been addressed with the addition of
Test coverage for new exception added in testBadUrl()
Comment #77
alexpottI was looking at whether we could use this in core... and I was thinking something along the lines of:
We should file a follow-up that will eventually allow is get rid of a preprocess hook in at the same time...
\Drupal\link\Hook\LinkThemeHooks::preprocessLinkFormatterLinkSeparateshould trigger a deprecation is $variables['link'] is not set\Drupal\link\Hook\LinkThemeHooks::themelink should be added to the variables\Drupal\link\Plugin\Field\FieldFormatter\LinkSeparateFormatter::viewElementsadd link to the render array.BUT!!!!!!
Then I noticed the following code in
\Drupal\link\Plugin\Field\FieldFormatter\LinkSeparateFormatter::viewElementsWhich makes this plan unworkable.
Given this and the fact that I can't work out where else to use this in core I wonder if adding this is actually necessary and whether other considers like trimming are always going to prevent it's usefulness?
Setting back to needs review to get an answer to that question.
Comment #78
smustgrave commentedMoving to NW as the MR appears to need a manual rebase, least gitlab isn't doing it right.
Also for an answer to #77, maybe sub-maintainer?
Comment #79
dcam commentedAs the subsystem maintainer I don't have an answer to #77. I don't know that anyone had a plan to use this in Core. Reading back through the comments, it sounds like this was something that developers of downstream code wanted. No one offered use cases. But it seemed simple enough that I thought it might be a worthwhile addition.
I rebased the MR. I'm setting the status back to Needs Review.
Comment #80
dcam commentedBecause this issue has opposing opinions from two framework managers (#37 and #77) I am tagging this for framework manager review.
Comment #82
larowlanfrom my point of view this is the companion to
LinkItemInterface::getUrl()- I have a lot of custom code that usesgetUrlandgetTitlewith a new Link so a utility to combine those is a useful addition in my opinionMost of our projects have this in a trait
EntityWithLinkFieldTraitthat we add on a per bundle class case.I think this is totally fine for @dcam as the subsystem maintainer to be the tie breaker.
Comment #83
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.