Problem/Motivation
There is a hook called hook_ranking(), which is invoked in \Drupal\node\Plugin\Search\NodeSearch::getRankings().
This is not a great name for a hook. It should be called hook_node_search_ranking() instead.
Proposed resolution
Deprecate hook_ranking
Rename to hook_node_search_ranking
Remaining tasks
Review
Commit
User interface changes
None.
API changes
hook_ranking() is deprecated in favor of hook_node_search_ranking(), but both are supported until Drupal 12.
Data model changes
None.
Original issue report (for readability of first few comments)
node and comment module's implement a 'hook_ranking' that is invoked by node modules' node_search_admin.
Related to #237748: Decouple core search module implementations from node and user module (and turn search/node and search/user to views)., I don't think this node_search_admin belongs in node module at all (belongs in search module). Regardless, hook_ranking just doesn't seem important enough to get away with violating namespace rules. It should be something like hook_node_search_ranking, and then if can get the search-related code out of node module, it can change to hook_search_ranking.
| Comment | File | Size | Author |
|---|
Issue fork drupal-1019966
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:
- 1019966-rename-hookranking-with
changes, plain diff MR !13185
Comments
Comment #1
webchick+100. I mentioned this back in the issue to document it but we only got documentation, not a hook rename. I think based on the argument that it isn't part of search module, like you said. So moving this entire chunk of code to search.module makes a lot of sense, too.
Tagging "DX".
Comment #2
xjm(Merging "node system" and "node.module" components for 8.x; disregard.)
Comment #3
jhodgdonSo. In 8.x, I do not think we can change this any more. It might have gotten noticed earlier if it was in the search.module component rather than buried in Node, because the Search maintainers did not know about it. Anyway, moving there and bumping to 9.x at this point.
Regarding the issue:
- Node search is now a plugin that lives in Node module, and this hook is definitely only for node searches. So the hook definitely belongs in node module, not search module.
- I am in favor of renaming it hook_node_search_ranking().
Comment #4
webchickI think as long as we don't break BC (leave the old hook name intact) it would be fine to add an additional hook fired at the same spot with an improved name. Not 100% sure, but tentatively switching back to 8.2.x.
Comment #5
jhodgdonFair enough. Updating the issue summary, and I think this could then probably be a Novice issue, at least for some novice coders... seems pretty straightforward.
Comment #6
leolandotan commentedSeems interesting! This will be my first issue like this. I'm gonna try it.
Comment #7
leolandotan commentedHere I have worked and followed the steps provided from the description and branched-off from 8.2.x too.
Special emphasis on
Hope everything are in order.
Thanks!
Comment #8
leolandotan commentedComment #9
jhodgdonWow, looks perfect, on the first try! All of the tests also passed, and we have extensive tests for all the node search ranking options. Great work, thanks! I did make some small edits to the change record.
Oh.... One thing I forgot in the task list... I guess we need to file an issue for 9.x to remove the hook, and add a @todo comment to the line of code in NodeSearch that invokes the old hook. The comment should say something like:
@todo Remove this in 9.0. Issue: [link to the new issue]
Would you like to file that issue and make the small revision on the patch? Sorry for not thinking of it earlier. ;)
Maybe the line of code would be easier to remove if the order was changed too, so the new hook is invoked first, and the old hook's results are added in afterwards?
Comment #10
leolandotan commentedThank you very much @jhodgdon! Was quite scary! :)
I will work on the additional tasks.
Comment #11
leolandotan commentedHi @jhodgdon! Here I have added your recommendations from comment #9.
Tasks done
Hope everything is in order.
Thanks!
Comment #12
leolandotan commentedComment #13
jhodgdonGreat! The new issue looks good. I made these two issues related (on the other one), and also edited the change record to reference the second issue (a change record can reference more than one issue).
By the way, a better way to make a link to an issue when you are editing a page or comment on drupal.org is to put something like
#2690437: Remove the deprecated hook_ranking() and the code invoking itin the text. So instead of a simple link like https://www.drupal.org/node/2690437, you get a link that looks like #2690437: Remove the deprecated hook_ranking() and the code invoking it... a nice trick. ;)And... Just one more small suggestion on the patch:
I don't think we need a comment saying we are adding results together using array addition. We generally want in-code comments that tell us why things are being done they way they are, not comments that just tell us what the code is doing if it's pretty obvious (as I think it is here).
So... I think just take out the first comment completely.
For the second one, maybe instead of "Remove this" say "Remove backwards-compatible invocation of deprecated hook", so it's clearer why we are removing the line?
Thanks! You're doing great!
Comment #14
jhodgdonAlso added link to new issue to issue summary, for completeness.
Comment #15
leolandotan commentedThanks for your review and encouragement @jhodgdon!
That link trick is awesome! A very good shortcut and more informative. I'll definitely use that. :) Got it on the "tell us why things are being done they way they are" rather than "comments that just tell us what the code is doing" too. I'll keep that in mind.
I will work on these recommended changes.
Comment #16
leolandotan commentedHere is the recommendations from @jhodgdon.
Things done
@tododescription.Thanks!
Comment #17
jhodgdonLooking great!
So... I took one final look through this and found a couple of small things to fix, that I (sorry!) missed in my earlier reviews... and then I think we'll be done.
The second line here needs to be indented 2 spaces.
As long as we are fixing things...
item id => item ID
Also the first comment line here exceeds 80 characters, so can you rewrap these two lines?
And rewrap this too, please.
I don't think we should indent "Issue:" in the second line here.
Comment #18
leolandotan commentedThanks for your review and no problem at all! :) I'll work on these items.
Comment #19
leolandotan commentedHere I have applied the recommendations from @jhodgdon.
Thanks!
Comment #20
jhodgdonThanks! I think this is ready to go now.
Comment #21
alexpottAre we sure that this is the right way to merge the results? Inside
invokeAllit does a$return = NestedArray::mergeDeep($return, $result);... I think this code should look more like this.I think we should be doing
This way the new hook is preferred and results are merged correctly. We still have a theoretical problem - say moduleX was changing the ranking the node module provides by implementing hook_ranking and having a higher weight than node... this change will break that.... hmmm... so maybe
is correct.
Comment #22
alexpottAlso as we're implementing a bc layer we should a test of it.
Comment #23
alexpottComment #24
jhodgdonFor testing: I suggest that we leave the comment module using the deprecated old hook for now, since we have coverage for comment, node, and statistics module hooks already.
For merging: for this particular hook I cannot see a reason that we need to do anything more than a simple array addition.
Comment #25
alexpott@jhodgdon I think that rather than leaving a deprecated usage around we should add an explicit test - we should be able to add the implementation to the node_test module.
Also we can't do simple array addition because that's not what is done internally in ModuleHandler::invokeAll()
Comment #29
ivan berezhnov commentedComment #33
andypostComment #35
mradcliffeI'm removing the novice tag on this issue temporarily, and I added the needs issue summary update because the proposed resolution should be updated based on alexpott's comments regarding adding a test.
Comment #43
mstrelan commentedMoving back to node module as per #3 and to reflect the patch. Ultimately I think search related functionality should be removed from node module, particularly if search gets deprecated in core, in favour of search api in drupal cms.
Comment #44
acbramley commentedI agree with moving search stuff to the search module, but it looks like this hook is directly invoked from the node_search search plugin, so maybe it does belong in node? Who knows how often this stuff is actually used though, maybe we can eventually move node search stuff to a node_search contrib module.
In any case, this needs a reroll on to an MR which is going to require quite a bit of rework for OOP hooks I imagine.
Comment #46
acbramley commentedComment #47
acbramley commentedComment #48
smustgrave commented$this->rankings += $this->moduleHandler->invokeAllDeprecated('Use hook_node_search_ranking() instead. See https://www.drupal.org/node/2690393.', 'ranking');Nice! not see that one before.
CR is straight forward and seems like a good deprecation and conversion. Don't see anything off.
Comment #49
alexpottCommitted 85aac9b and pushed to 11.x. Thanks!
Comment #53
nicxvan commentedSince this deprecates a hook, I'd love opinions on an issue to change how we do that: #3001190: Deprecate ModuleHandlerInterface::*Deprecated() in favor of #[DeprecatedHook]