You are here

function mostpopular_match_result_nodes in Drupal Most Popular 7

Same name and namespace in other branches
  1. 6 mostpopular.api.php \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 an entity, returns an array containing:

  • entity_type: the type of entity.
  • entity_id: the ID of the entity.
  • title: the title of the entity, fetched from the entity itself.
  • url: the external URL of the entity.
  • path: the internal Drupal path of the entity.
  • count: the number of times the entity was referenced.

Otherwise, returns NULL.

4 calls to mostpopular_match_result_nodes()
callback_mostpopular_refresh_viewed in ./mostpopular.api.inc
Implements the 'refresh_$delta' callback.
mostpopular_addthis_refresh_shared in modules/mostpopular_addthis/mostpopular_addthis.module
Implements the 'refresh_delta' callback for the AddThis.com analytics service.
mostpopular_disqus_refresh_commented in modules/mostpopular_disqus/mostpopular_disqus.module
Implements the 'refresh_delta' callback for the Disqus service.
mostpopular_ga_refresh_viewed in modules/mostpopular_ga/mostpopular_ga.module
Implements the 'refresh_delta' callback for the GA mostpopular viewed service.

File

./mostpopular.module, line 1174
The main file for the Most Popular module.

Code

function mostpopular_match_result_nodes($url, $count, $options = array()) {
  $options += array(
    'entities_only' => FALSE,
    'entity_types' => array(),
  );
  $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 = variable_get('mostpopular_exclude_paths', '');
  if (empty($url) || drupal_match_path($url, $excludepaths)) {
    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 (drupal_match_path($path, $excludepaths)) {
    return NULL;
  }
  $out = (object) array(
    'path' => $path,
    'count' => $count,
  );

  // Attempt to lookup the entity
  $item = menu_get_item($path);
  $entity = NULL;
  if (!empty($item['load_functions'])) {
    foreach ($item['load_functions'] as $i => $func) {
      if ($func == 'menu_tail_load') {
        break;
      }

      // Extract the entity type from the name of the load function
      $entity_type = substr($func, 0, -5);

      // Compare this to the list of valid entity types
      if (empty($options['entity_types']) || isset($options['entity_types'][$entity_type])) {

        // Load the entity
        $parts = explode('/', $path);
        if (isset($parts[$i]) && function_exists($func)) {
          $entity = $func($parts[$i]);

          // Check that the bundle matches
          if (isset($entity) && is_object($entity)) {
            list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
            if (!empty($options['entity_types']) && !isset($options['entity_types'][$entity_type][$bundle])) {
              $entity = NULL;
            }
          }
          else {
            $entity = NULL;
          }
        }
      }
      break;
    }
  }
  if (isset($entity)) {

    // Check that anonymous users have access to view this entity
    $access = entity_access('view', $entity_type, $entity, user_load(0));
    if (isset($access) && $access === FALSE) {
      return NULL;
    }
    $out->entity_type = $entity_type;
    $out->entity_id = entity_id($entity_type, $entity);
    $out->title = entity_label($entity_type, $entity);
    $uri = entity_uri($entity_type, $entity);
    if (isset($uri['path'])) {
      $out->path = $uri['path'];
    }
  }
  if ($entity || !$options['entities_only']) {
    return $out;
  }
  return NULL;
}