You are here

function mostpopular_match_result_nodes in Drupal Most Popular 6

Same name and namespace in other branches
  1. 7 mostpopular.module \mostpopular_match_result_nodes()

Matches the given URL to a Drupal node, resolving aliases appropriately. The homepage will never be included in this list.

The URL can be an internal URL or it can start with one of the configured Drupal base paths, which will be stripped from the URL before the alias is resolved.

If the URL corresponds to a node, an array will be returned with properties of that node from the most popular service.

Parameters

string $url: A URL to match. This can be either an internal Drupal URL or it can start with one of the configured site basepaths.

integer $count: The number of times this node appears.

Return value

array If the url corresponds to a node, returns an array containing:

  • nid: the nid of the node.
  • title: the title of the node, fetched from the node itself.
  • url: the internal Drupal path of the node.
  • count: the number of times the node was referenced by the service.

Otherwise, returns NULL.

4 calls to mostpopular_match_result_nodes()
hook_mostpopular_service in ./mostpopular.api.php
Defines hook_mostpopular_service() and provides an empty implementation.
mostpopular_addthis_mostpopular_service in modules/mostpopular_addthis/mostpopular_addthis.module
Implements hook_mostpopular_service().
mostpopular_drupal_mostpopular_service in modules/mostpopular_drupal/mostpopular_drupal.module
Implements hook_mostpopular_service().
mostpopular_ga_mostpopular_service in modules/mostpopular_ga/mostpopular_ga.module
Implements hook_mostpopular_service().

File

./mostpopular.api.php, line 210
Provides functions for other modules to use to interact with the Most Popular data.

Code

function mostpopular_match_result_nodes($url, $count) {
  $url = trim($url);

  // Strip out the base path from the URL.
  $basepaths = variable_get('mostpopular_basepaths', array());
  foreach ($basepaths as $base) {
    if (stripos($url, $base) === 0) {
      $url = drupal_substr($url, drupal_strlen($base));
      break;
    }
  }

  // Strip off any leading slashes
  if (stripos($url, '/') === 0) {
    $url = drupal_substr($url, 1);
  }

  // If the URL points to an excluded path, ignore it.
  $excludepaths = array_flip(variable_get('mostpopular_exclude_paths', array()));
  if (empty($url) || isset($excludepaths[$url])) {
    return NULL;
  }

  // Get the internal path for the URL alias.
  $path = drupal_get_normal_path($url);

  // If the URL points to an excluded path, ignore it.
  if (isset($excludepaths[$path])) {
    return NULL;
  }

  // Return only the paths that point to nodes.
  if (preg_match('/^\\/?node\\/(\\d+)$/', $path, $matches)) {
    $nid = (int) $matches[1];
    $node = node_load($nid);
    if (!empty($node)) {
      return array(
        'nid' => $nid,
        'title' => $node->title,
        'url' => "node/{$nid}",
        'count' => $count,
      );
    }
  }
  return NULL;
}