function NodeHooks1::ranking

Implements hook_ranking().

Attributes

#[Hook('ranking')]

File

core/modules/node/src/Hook/NodeHooks1.php, line 216

Class

NodeHooks1
Hook implementations for node.

Namespace

Drupal\node\Hook

Code

public function ranking() : array {
  // Create the ranking array and add the basic ranking options.
  $ranking = [
    'relevance' => [
      'title' => $this->t('Keyword relevance'),
      // Average relevance values hover around 0.15
'score' => 'i.relevance',
    ],
    'sticky' => [
      'title' => $this->t('Content is sticky at top of lists'),
      // The sticky flag is either 0 or 1, which is automatically normalized.
'score' => 'n.sticky',
    ],
    'promote' => [
      'title' => $this->t('Content is promoted to the front page'),
      // The promote flag is either 0 or 1, which is automatically normalized.
'score' => 'n.promote',
    ],
  ];
  // Add relevance based on updated date, but only if it the scale values have
  // been calculated in node_cron().
  if ($node_min_max = \Drupal::state()->get('node.min_max_update_time')) {
    $ranking['recent'] = [
      'title' => $this->t('Recently created'),
      // Exponential decay with half life of 14% of the age range of nodes.
'score' => 'EXP(-5 * (1 - (n.created - :node_oldest) / :node_range))',
      'arguments' => [
        ':node_oldest' => $node_min_max['min_created'],
        ':node_range' => max($node_min_max['max_created'] - $node_min_max['min_created'], 1),
      ],
    ];
  }
  return $ranking;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.