Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Split from #2091399: [META] Remove menu_get_object()
Comment | File | Size | Author |
---|---|---|---|
#43 | menu_get_object-2095959-43-interdiff.txt | 3.82 KB | Berdir |
#43 | menu_get_object-2095959-43.patch | 18.27 KB | Berdir |
#39 | interdiff.txt | 2.61 KB | dawehner |
#39 | menu_get_object-2095959.patch | 14.97 KB | dawehner |
#31 | interdiff.txt | 1.39 KB | dawehner |
Comments
Comment #1
ekes CreditAttribution: ekes commentedPatch replaces all uses of menu_get_object('node') which are not in views arguments.
Views arguments should probably follow the same pattern as #2095961: Remove instances of menu_get_object('user') should that one make sense.
One function to note in this patch
node_block_access()
also replaces the existing arg() code. It would be nice to also use $request->attributes but the original logic covers any page where there is a node object, but only the node/add/{node_type} page not other instances with {node_type} (ie content type admin pages). Hence the ugly call to get the route.Comment #3
tim.plunkettYou can't do this for node_is_page(), because on something like a comment page that has the node in the request context, this will be a false positive. In some of these places, you actually need to check the route name.
If you reread the "menu_get_object() is used for two main reasons:" from the parent/meta issue, this is #1
Comment #4
ekes CreditAttribution: ekes commentedThere are a few more complicated instances on this one (than the user, and taxo ones). First
node_is_page()
, which then uses menu_get_object, is now only used three times in core.Once by statistics module:
This actually wants specifically the route node.view and nothing else.
Once in book module:
I'm not sure here if fixing this to the route makes sense? Should the links be made available even if the site is configured to show the full node on some other route?
And finally in the node module itself:
I guess this wants the route node.view; and again no others?
Comment #5
ekes CreditAttribution: ekes commentedSo working on the common cases.
Comment #6
ekes CreditAttribution: ekes commentedUpdating for dependency injection where possible.
Comment #10
ekes CreditAttribution: ekes commentedPrevious patch is failing because the test was passing with the block shown on a 403 page.
The test has an admin user with 'administer content' privs, but no 'create * content' access. The test was checking with this user if the block was shown on the front page - correctly no; and then on the 'node/add/article' page which is returning a 403 access denied. Previous code still showed the block. Updating the test to check on the 'node/add/article' page with the webuser which does have access to 'create article content'.
Comment #11
dawehnerIf we change this line anyway we should use $this->t() instead.
What a win if you compare the two ones.
Comment #12
ekes CreditAttribution: ekes commentedAdding in swapping t() for $this->t() in the Date Argument class.
Comment #13
dawehnerThank you
Comment #14
catchThis is quite ugly, and another good reason to do #2124749: [meta] Stop using $request->attributes->get(MAGIC_KEY) as a public API, but it's not the fault of this patch.
This reference should be removed.
Comment #15
jmuzz CreditAttribution: jmuzz commentedYes a function's comment shouldn't explain implementation issues like what functions it calls.
Also updated the patch to apply to the most recent version of the repository.
Comment #16
jmuzz CreditAttribution: jmuzz commentedComment #17
star-szrTagging for reroll.
Comment #18
InternetDevels CreditAttribution: InternetDevels commentedrerolled patch
Comment #19
dawehnerThank you for taking the time to inject things properly.
Comment #20
catchWhat else would $node be?
Not sure I like exposing the Symfony CMF RouteObjectInterface for such a common operation here.
Comment #21
webchickComment #22
InternetDevels CreditAttribution: InternetDevels commented$request->attributes->get(RouteObjectInterface::ROUTE_NAME)
is used in couple placesblock_page_build
TermBreadcrumbBuilder::applies
menu_link_system_breadcrumb_alter
\Drupal\forum\ForumBreadcrumbBuilder
and few more.
And these are in core. Why is usage of RouteObjectInterface common in node.module?
Comment #23
dawehnerYeah technically the code previous did just checked whether the nid at position 1 in the path agrees with the nid passed in, so yeah I guess we can keep the same logic, too.
Comment #24
dawehnerSo needs work, let's just check whether the node object exists and confirms with the passed in one.
Comment #25
BerdirAccording to #2177041: Remove all implementations of hook_menu, that issue is blocked by this, which makes this a beta blocker?
Comment #26
BerdirAm I the only one that thinks the Request object has a weird API? :)
Comment #27
BerdirActually, get() returns NULL by default (you can even control what is returned by default), that would be much easier than the conditionals that we have here?
Instead of this, we just write:
Which is almost as easy as before, unlike the current change?
Would also save some of the introduced !empty($node) checks, which I always find weird (having a defined variable and if ($node) is imho easier to read, what is an "empty" $node ;)).
Comment #28
dawehnerGood ideas, seriously.
Comment #30
BerdirThat looks much better I think :)
Those default argument plugins are pretty crazy, how they just fall back to the node fields and try ti find some term ID's in whatever way they can :)
Comment #31
dawehnerembarrassing
Comment #33
dawehner31: menu_get_object-node-2095959-31.patch queued for re-testing.
Comment #34
BerdirI'm not sure about having comments that reference previous code. It might make sense in the interdiff, but for someone looking at the resulting code (in a year, or two), it won't.
So maybe simply move the first comment line into the if or rewrite (Check if this is...) and then replace the second part of the second sentence with "so check specifically for the node.add route" ?
Ok, that's too complicated, I mean something like this:
And as ugly as that route name check is, I agree with #22 that it's already used in HEAD and shouldn't block this, but maybe we can open a followup issue? I think it's really unfortunate that we have to expose the fact that we are using Symfony/Cmf for the routing for a simple (?) check like this.
Comment #35
twistor CreditAttribution: twistor commentedBookNavigationBlock
Node object.
How come we check for instanceof NodeInterface some places and not others?
Tid object.
Date object.
Comment #36
catchI'm not sure this is really the same as the existing usages.
menu_get_object() is a commonly used API in contrib/custom modules, and we're replacing it with something that is extremely ugly here. If we open a follow-up to fix that, it'll be two API changes instead of one (and that follow-up would probably need to be critical/beta-blocker). The current usages are at least mostly implementation details/crept in - i.e. I don't think we have this in a change notice yet.
Comment #37
dawehnerThis issue is blocking #2177041: Remove all implementations of hook_menu and so a full chain of related issues. Afaik there was an issue which did nothing else then provide a service which allows you to fetch the route name for example from a service instead from the request object.
Comment #38
tim.plunkettComment #39
dawehnerThis makes it both safer but also more suitable for static code analyze tools/IDEs.
Comment #40
tim.plunkettWe have #2124749: [meta] Stop using $request->attributes->get(MAGIC_KEY) as a public API. That doesn't actually encompass these constants, but it could.
This is why I originally opened #2103301: Add a helper class to introspect a given request.
But I don't think we should hold this issue up on that, this is a huge blocker for the menu system changes.
Comment #41
xjmHm, I'm not sure #36 is addressed entirely. I can understand the desire to get this in and unblock the rest of the MenuSystemRevamp, but I'd want, at a minimum, an explicit beta blocker followup if we're not going to fix it here.
Edit: I missed that catch tagged #2124749-15: [meta] Stop using $request->attributes->get(MAGIC_KEY) as a public API. On the fence as to whether that covers entirely though.
Comment #42
xjmLet's do this.
Comment #43
BerdirLooks like we forgot two calls, one of them was actually converted below :)
Given that these were the last usages of that function, let's just drop it and close the meta?
Comment #44
BerdirDidn't mean to leave it at RTBC.
Comment #45
dawehnerGood catch!
Comment #46
catchI'm OK doing this in the other issue - exactly the same lines of code need to be touched, even if it's not quite the same problem with exposing the Symfony CMF interface. Committed/pushed to 8.x, thanks!
Needs a change notice, we should explicitly state that the API might change when #2124749: [meta] Stop using $request->attributes->get(MAGIC_KEY) as a public API is resolved though.
Comment #47
dawehnerThank you, here is a change notice: https://drupal.org/node/2192317
Comment #48
BerdirSlightly updated and published.
Comment #49
star-szr