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.

Issue fork drupal-1019966

Command icon 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

webchick’s picture

+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".

xjm’s picture

Component: node.module » node system
Issue summary: View changes

(Merging "node system" and "node.module" components for 8.x; disregard.)

jhodgdon’s picture

Version: 8.0.x-dev » 9.x-dev
Component: node system » search.module

So. 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().

webchick’s picture

Version: 9.x-dev » 8.2.x-dev

I 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.

jhodgdon’s picture

Issue summary: View changes
Issue tags: +Novice

Fair 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.

leolandotan’s picture

Assigned: Unassigned » leolandotan

Seems interesting! This will be my first issue like this. I'm gonna try it.

leolandotan’s picture

StatusFileSize
new6.27 KB

Here 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!

leolandotan’s picture

Assigned: leolandotan » Unassigned
Status: Active » Needs review
jhodgdon’s picture

Title: Rename hook_ranking » Rename hook_ranking (with BC layer)
Status: Needs review » Needs work

Wow, 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?

leolandotan’s picture

Assigned: Unassigned » leolandotan

Thank you very much @jhodgdon! Was quite scary! :)

I will work on the additional tasks.

leolandotan’s picture

Status: Needs work » Needs review
StatusFileSize
new6.63 KB
new1.07 KB

Hi @jhodgdon! Here I have added your recommendations from comment #9.

Tasks done

  • File an issue for 9.x to remove the hook here https://www.drupal.org/node/2690437. I hope it's to add this to this ticket's related issue.
  • Add a @todo comment to the line of code in NodeSearch that invokes the old hook.
  • 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?

Hope everything is in order.

Thanks!

leolandotan’s picture

Assigned: leolandotan » Unassigned
jhodgdon’s picture

Status: Needs review » Needs work

Great! 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 it in 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:

+++ b/core/modules/node/src/Plugin/Search/NodeSearch.php
@@ -736,14 +736,17 @@ protected function parseAdvancedDefaults($f, $keys) {
+      // Add the results together using array addition.
+      $this->rankings = $this->moduleHandler->invokeAll('node_search_ranking');
+      // @todo Remove this in 9.0. Issue: https://www.drupal.org/node/2690437.
+      $this->rankings += $this->moduleHandler->invokeAll('ranking');

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!

jhodgdon’s picture

Issue summary: View changes

Also added link to new issue to issue summary, for completeness.

leolandotan’s picture

Assigned: Unassigned » leolandotan

Thanks 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.

leolandotan’s picture

Assigned: leolandotan » Unassigned
Status: Needs work » Needs review
StatusFileSize
new6.63 KB
new871 bytes

Here is the recommendations from @jhodgdon.

Things done

  • Removed the previous comment for the new hook's line of code.
  • Updated the @todo description.

Thanks!

jhodgdon’s picture

Status: Needs review » Needs work

Looking 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.

  1. +++ b/core/modules/node/node.api.php
    @@ -442,6 +442,9 @@ function hook_node_update_index(\Drupal\node\NodeInterface $node) {
    + * @deprecated in Drupal 8.2.0 and will be removed before Drupal 9.0.0. Use
    + * hook_node_search_ranking() instead.
    

    The second line here needs to be indented 2 spaces.

  2. +++ b/core/modules/node/node.api.php
    @@ -469,6 +472,73 @@ function hook_ranking() {
    +        // Note that we use i.sid, the search index's search item id, rather than
    +        // n.nid.
    

    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?

  3. +++ b/core/modules/node/node.api.php
    @@ -469,6 +472,73 @@ function hook_ranking() {
    +        // The highest possible score should be 1, and the lowest possible score,
    +        // always 0, should be 0.
    

    And rewrap this too, please.

  4. +++ b/core/modules/node/src/Plugin/Search/NodeSearch.php
    @@ -751,14 +751,17 @@ protected function parseAdvancedDefaults($f, $keys) {
    +      // @todo Remove backwards-compatible invocation of deprecated hook in 9.0.
    +      //   Issue: https://www.drupal.org/node/2690437.
    

    I don't think we should indent "Issue:" in the second line here.

leolandotan’s picture

Assigned: Unassigned » leolandotan

Thanks for your review and no problem at all! :) I'll work on these items.

leolandotan’s picture

Assigned: leolandotan » Unassigned
Status: Needs work » Needs review
StatusFileSize
new7.49 KB
new3.17 KB

Here I have applied the recommendations from @jhodgdon.

Thanks!

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! I think this is ready to go now.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/modules/node/src/Plugin/Search/NodeSearch.php
@@ -751,14 +751,17 @@ protected function parseAdvancedDefaults($f, $keys) {
+      $this->rankings = $this->moduleHandler->invokeAll('node_search_ranking');
+      // @todo Remove backwards-compatible invocation of deprecated hook in 9.0.
+      // Issue: https://www.drupal.org/node/2690437.
+      $this->rankings += $this->moduleHandler->invokeAll('ranking');

Are we sure that this is the right way to merge the results? Inside invokeAll it does a $return = NestedArray::mergeDeep($return, $result);... I think this code should look more like this.

I think we should be doing

$this->rankings = $this->moduleHandler->invokeAll('ranking');
$this->rankings = NestedArray::mergeDeep($this->rankings, $this->moduleHandler->invokeAll('node_search_ranking'));

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

$this->rankings = $this->moduleHandler->invokeAll('node_search_ranking');
$this->rankings = NestedArray::mergeDeep($this->rankings, $this->moduleHandler->invokeAll('ranking'));

is correct.

alexpott’s picture

Issue tags: +Needs tests

Also as we're implementing a bc layer we should a test of it.

alexpott’s picture

Status: Needs review » Needs work
jhodgdon’s picture

For 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.

alexpott’s picture

@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()

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ivan berezhnov’s picture

Issue tags: +CSKyiv18

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

mradcliffe’s picture

I'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.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mstrelan’s picture

Component: search.module » node system

Moving 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.

acbramley’s picture

Issue tags: +Needs reroll

I 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.

acbramley’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs tests, -Needs issue summary update, -Needs reroll
acbramley’s picture

Title: Rename hook_ranking (with BC layer) » Rename and deprecate hook_ranking
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

$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.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 85aac9b and pushed to 11.x. Thanks!

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

  • alexpott committed 85aac9bd on 11.x
    Issue #1019966 by webchick, jhodgdon, leolandotan, alexpott, acbramley,...

nicxvan’s picture

Since 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]

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.