Problem/Motivation
As a result of #2339219: [meta] Finalize URL generation API (naming, docs, deprecation), we've standardized on the \Drupal\Core\Url object as The Way™ to deal with URLs. Even \Drupal::l() takes a Url object.
However, the most common way of printing links, '#type' => 'link'
, does not support a URL. Instead, they still support the legacy #href, as well as the intermediary #route_name/#route_parameters pair.
This also extends to the very common '#theme' => 'links'
, which internally uses '#type' => 'link'
.
Proposed resolution
Remove support for both #href
and #route_name
from render arrays using '#type' => 'link'
Remove support for both href
and route_name
from render arrays using '#theme' => 'links'
Remaining tasks
None
User interface changes
API changes
- Url::toArray() and Url::toRenderArray() now return an array containing the Url object itself, and are largely unnecessary now.
- Render arrays using
'#type' => 'link'
and'#theme' => 'links'
no longer support href or route_name, only #url/url - \Drupal\language\LanguageSwitcherInterface::getLanguageSwitchLinks() and \Drupal\Core\Language\LanguageManagerInterface::getLanguageSwitchLinks() take a Url object instead of a path.
- \Drupal\Core\Menu\MenuLinkInterface::getDeleteRoute(), \Drupal\Core\Menu\MenuLinkInterface::getEditRoute(), and \Drupal\Core\Menu\MenuLinkInterface::getTranslateRoute() return a Url object instead of an array
- The config_path annotation used for LanguageNegotiationMethodInterface plugins is now config_route_name
Comment | File | Size | Author |
---|---|---|---|
#84 | interdiff.txt | 875 bytes | tim.plunkett |
#84 | 2347465-type-link-84.patch | 195.61 KB | tim.plunkett |
#82 | 2347465-type-link-82.patch | 195.6 KB | tim.plunkett |
#82 | interdiff.txt | 4.27 KB | tim.plunkett |
#79 | interdiff.txt | 776 bytes | tim.plunkett |
Comments
Comment #1
dawehnerLet's start to see how much breaks with just that.
Working on actually fixing it.
Comment #3
dawehnerSome work done, more to come.
Comment #5
pwolanin CreditAttribution: pwolanin commentedcan you push a sandbox branch?
Comment #6
tim.plunkettExtra #href, one missing conversion.
What's the difference between these?
Comment #7
tim.plunkettWill we need https://www.drupal.org/node/2235495#comment-9197627 once this is in?
Comment #8
tim.plunkettWorking on this.
Comment #9
tim.plunkettWhew. Quite the can of worms.
Comment #11
tim.plunkettThat's probably it for me tonight.
Comment #13
tim.plunkettOne more for the road.
Comment #15
dawehnerThank you for taking this over yesterday, working on it now.
Comment #16
dawehnerLet's see how much else is left.
Comment #18
dawehnerLet's quickly reroll it. I consider it critical to actually ensure that our route based system works.
Comment #20
dawehnerDoh, so unproductive!
Comment #21
tim.plunkettThanks @dawehner, those fixes should help a lot. My turn again!
Comment #23
tim.plunkettComment #25
tim.plunkett#2345753: Remove url(current_path()) and url(NULL) with <current> and <none> might have gotten in the way if we did it later, so pulling parts of it in.
I'm still working on this, in a test issue.
Comment #26
tim.plunkettThis incorporates the first half of #1853072: Remove forum_menu_local_tasks_alter() hack and instead add links in ForumController::build
Comment #28
tim.plunkettI can't deal with this issue more tonight. Good luck $whoever_is_next!
Comment #29
tim.plunkettOkay a bit more before bed.
Comment #31
dawehnerWorking on it a bit.
Comment #32
tim.plunkettThat was about 3 hours ago, and you're not online, so I'm going to get back to posting patches for this. Hopefully we don't overlap too much.
Comment #34
tim.plunkettComment #36
tim.plunkettStill working on this. Getting close!
Comment #37
tim.plunkettHmmm, UnroutedUrlAssembler::buildLocalUrl needs to do more of what UrlGenerator::generateFromPath does...
Comment #38
tim.plunkettComment #39
tim.plunkettHere it is without the exceptions I used to track things down.
Comment #42
tim.plunkettIt's the little things.
Comment #43
jibranWe need an issue for this.
Comment #44
dawehnerLet's see how much this stops.
Comment #46
tim.plunkettWorking on this
Comment #47
tim.plunkettKeep in mind this still removes CommentLinkBuilderTest, which I have NO idea how to fix.
Comment #48
tim.plunkettYay! I was able to figure out CommentLinkBuilderTest. It was easier to debug when I reduced the permutations down to 1 each, and found the exact combination that was failing.
Moral of the story: don't mock value objects.
(Also, I switched the order of the assertion since PHPUnit makes a distinction between expected/actual.
Comment #49
tim.plunkettAPI-wise, this should have been a beta blocker. Better late than never.
Comment #50
tim.plunkettWeird tag bug.
Comment #51
pwolanin CreditAttribution: pwolanin commentedshoudl we remove toArray() and toRenderArray() all together?
Comment #52
dawehnerI decided to not do that for the reason of release management, deprecated can be removed and decided later.
@tim
No attack here on your work, some changes are needed, some are for research.
Comment #54
tim.plunkettJust as long as it gets back to green before long, research away.
I removed verbose calls because we don't usually force debug output into our tests, and because it blows up on Url
Comment #55
dawehnerWell, I hate to remove stuff which is a) helpful and b) we should fix the actual underlying problem, don't we? (yeah var_export is in general a bad idea).
Comment #56
alexpott?
Comment #57
dawehnerThis is not needed any longer.
Comment #59
cilefen CreditAttribution: cilefen commentedRerolled because #2326409: Annotate render element plugins removed hook_element_info() from the toolbar.module.
Comment #60
dawehnerRERTBC
Comment #61
alexpottI think we've agreed that path processing will not be done for unrouted url's.
Comment #62
tim.plunkettOur current implementation of shortcut paths considers them unrouted, by using base://, and they need language processing. Otherwise, someone needs to rewrite shortcut module, I guess?
Comment #63
tim.plunkettThis needs to be reverted, we just removed support for 'route_name'.
I don't know why we're bringing this back.
Comment #64
tim.plunkettComment #66
tim.plunkettForgot the interdiff, and wasn't up-to-date with HEAD.
Comment #67
tim.plunkettOh, actually this should work. Forgot that all of the routing changes were done to Shortcut, and the path is just computed.
Comment #69
tim.plunkettWeird. TypedData is adding an extra
'value' => []
to the route_parameters, which breaks the comparisonif ($shortcut->getRouteName() == $url->getRouteName() && $shortcut->getRouteParams() == $url->getRouteParameters()) {
Why that is happening now and not in HEAD, I do not know.
Comment #71
tim.plunkettOh! That would be why. Shortcut::setRouteParams is wrapping the array in an array for no reason.
Yay for finding bugs.
Comment #72
tim.plunkettThis should adequately address @alexpott's feedback.
Comment #73
alexpottThis seems to be the wrong way around?
I need more time to review this patch.
Comment #74
pwolanin CreditAttribution: pwolanin commentedShortcut thinks should use routes - or if not, they should not get any path processing applied.
Anything using base:// is like hard-coding a link.
Comment #75
catchI'd like to hold off committing this patch until we've got solid agreement on #2351379: [meta] Define, then support exact use cases for link generation and storage.
After that while I expect we'll need to convert a lot of these cases, we might find specific examples that need to be moved to non-routes. Or if we do go ahead with the patch, we might have to go through and convert some cases a second time.
Comment #76
tim.plunkett#74, I fixed shortcut, that's using routes, and I removed the path processing I added.
The only usages of base:// added in this patch are:
Drupal\views\Plugin\views\Field\Links::getLinks() where we deal with a string path
Two places we handle ?destination=, which must be a string path.
If you hold off on this patch past Thursday, you'll need to find someone else to work on it, I'll be gone until late October after this week. It'd be a shame to see this massive improvement fall stale and not get in.
Comment #77
catch@Tim can you look at #2351379: [meta] Define, then support exact use cases for link generation and storage then? It has a comprehensive issue summary and no big long discussion to wade through. Also enjoy the break :)
Comment #78
tim.plunkettI think this issue makes that one easier, not harder.
Comment #79
tim.plunkettRerolled and fixed #73. Opened #2352791: Add test coverage for admin/structure/block/demo
Comment #81
catchLooking at the patch I do think this makes the other issue easier rather than harder.
Nearly all the changes are the specific use case of linking to a route very much coupled to a module, it's not the user-entered route use-cases that are proving difficult to support with routes in the other issue. Also some valid uses of base::// for destination which I don't think we'll need to change either.
So yes (unless someone has a concrete objection I missed), let's keep going here and continue in the other issue dealing with menu links/shortcuts and similar cases where the pain is.
Comment #82
tim.plunkettAdded a workaround for a random bug.
I tested the patch in #2352791: Add test coverage for admin/structure/block/demo with the patch from #71, and it catches the bug, and passes with the fix in #79.
Additionally, added @todos for the known places we'd want to stop using with fromUri().
Comment #83
dawehnernote: you have to use 'query' => ['parent' => $node->id()) here.
I am not convinced by that change. This is highly problematic as this functions are executed during routing rebuild.
Comment #84
tim.plunkettNone of the get*Path() methods are called during routing, just get*RouteName() and get*Route().
Opened #2353029: Book "Add child page" link query string needs test coverage to add test coverage.
Comment #85
dawehnerAh I see,
Comment #86
alexpottThanks for opening the issues to improve test coverage.
I agree with @catch in #81. I don't any reason to hold this up based on #2351379: [meta] Define, then support exact use cases for link generation and storage
Comment #87
alexpottCommitted 195d499 and pushed to 8.0.x. Thanks!
Comment #89
hass CreditAttribution: hass commentedCan we change this wording a bit, please? I have read it 3-4 times to understand that this is not a typo. Maybe something like "
If omitted, no <a>-tag is printed out.
"?Comment #90
xjmI'm guessing #82 missed an instance of the random fail and so this introduced: #2353457: Random test failure in Drupal\views_ui\Tests\DisplayPathTest
Comment #91
catch#2353347: Random failure in DisplayPathTest
Comment #92
larowlanPublished change notice (tracked down to this after finding all my contrib list builder operations broken).
Comment #94
YesCT CreditAttribution: YesCT commentedoops, removed the deprecated toArray() from the test that was to test toArray(). see #2417445: Remove Url::toArray()