Unverified Commit 85aac9bd authored by Alex Pott's avatar Alex Pott
Browse files

Issue #1019966 by webchick, jhodgdon, leolandotan, alexpott, acbramley, jody...

Issue #1019966 by webchick, jhodgdon, leolandotan, alexpott, acbramley, jody lynn: Rename and deprecate hook_ranking
parent 5c1956d6
Loading
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -442,9 +442,9 @@ public function userPredelete($account): void {
  }

  /**
   * Implements hook_ranking().
   * Implements hook_node_search_ranking().
   */
  #[Hook('ranking')]
  #[Hook('node_search_ranking')]
  public function ranking(): array {
    return \Drupal::service('comment.statistics')->getRankingInfo();
  }
+73 −0
Original line number Diff line number Diff line
@@ -370,6 +370,10 @@ function hook_node_update_index(NodeInterface $node): string|\Stringable {
 *   - arguments: (optional) If any arguments are required for the score, they
 *     can be specified in an array here.
 *
 * @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Use
 *   hook_node_search_ranking() instead.
 * @see https://www.drupal.org/node/2690393
 *
 * @ingroup entity_crud
 */
function hook_ranking(): array {
@@ -398,6 +402,75 @@ function hook_ranking(): array {
  return $data;
}

/**
 * Provide additional methods of scoring for core search results for nodes.
 *
 * A node's search score is used to rank it among other nodes matched by the
 * search, with the highest-ranked nodes appearing first in the search listing.
 *
 * For example, a module allowing users to vote on content could expose an
 * option to allow search results' rankings to be influenced by the average
 * voting score of a node.
 *
 * All scoring mechanisms are provided as options to site administrators, and
 * may be tweaked based on individual sites or disabled altogether if they do
 * not make sense. Individual scoring mechanisms, if enabled, are assigned a
 * weight from 1 to 10. The weight represents the factor of magnification of
 * the ranking mechanism, with higher-weighted ranking mechanisms having more
 * influence. In order for the weight system to work, each scoring mechanism
 * must return a value between 0 and 1 for every node. That value is then
 * multiplied by the administrator-assigned weight for the ranking mechanism,
 * and then the weighted scores from all ranking mechanisms are added, which
 * brings about the same result as a weighted average.
 *
 * @return array
 *   An associative array of ranking data. The keys should be strings,
 *   corresponding to the internal name of the ranking mechanism, such as
 *   'recent', or 'comments'. The values should be arrays themselves, with the
 *   following keys available:
 *   - title: (required) The human readable name of the ranking mechanism.
 *   - join: (optional) An array with information to join any additional
 *     necessary table. This is not necessary if the table required is already
 *     joined to by the base query, such as for the {node} table. Other tables
 *     should use the full table name as an alias to avoid naming collisions.
 *   - score: (required) The part of a query string to calculate the score for
 *     the ranking mechanism based on values in the database. This does not need
 *     to be wrapped in parentheses, as it will be done automatically; it also
 *     does not need to take the weighted system into account, as it will be
 *     done automatically. It does, however, need to calculate a decimal between
 *     0 and 1; be careful not to cast the entire score to an integer by
 *     inadvertently introducing a variable argument.
 *   - arguments: (optional) If any arguments are required for the score, they
 *     can be specified in an array here.
 *
 * @ingroup entity_crud
 */
function hook_node_search_ranking(): array {
  $data = [];
  // If voting is disabled, we can avoid returning the array, no hard feelings.
  if (\Drupal::config('vote.settings')->get('node_enabled')) {
    $data += [
      'vote_average' => [
        'title' => t('Average vote'),
        // Note that we use i.sid, the search index's search item id, rather
        // than n.nid.
        'join' => [
          'type' => 'LEFT',
          'table' => 'vote_node_data',
          'alias' => 'vote_node_data',
          'on' => 'vote_node_data.nid = i.sid',
        ],
        // The highest possible score should be 1, and the lowest possible
        // score, always 0, should be 0.
        'score' => 'vote_node_data.average / CAST(%f AS DECIMAL)',
        // Pass in the highest possible voting score as a decimal argument.
        'arguments' => [\Drupal::config('vote.settings')->get('score_max')],
      ],
    ];
  }
  return $data;
}

/**
 * Alter the links of a node.
 *
+2 −2
Original line number Diff line number Diff line
@@ -46,9 +46,9 @@ public function cron(): void {
  }

  /**
   * Implements hook_ranking().
   * Implements hook_node_search_ranking().
   */
  #[Hook('ranking')]
  #[Hook('node_search_ranking')]
  public function ranking(): array {
    // Create the ranking array and add the basic ranking options.
    $ranking = [
+2 −1
Original line number Diff line number Diff line
@@ -720,7 +720,8 @@ protected function parseAdvancedDefaults($f, $keys) {
   */
  protected function getRankings() {
    if (!$this->rankings) {
      $this->rankings = $this->moduleHandler->invokeAll('ranking');
      $this->rankings = $this->moduleHandler->invokeAll('node_search_ranking');
      $this->rankings += $this->moduleHandler->invokeAllDeprecated('Use hook_node_search_ranking() instead. See https://www.drupal.org/node/2690393.', 'ranking');
    }
    return $this->rankings;
  }