You are here

function views_navigation_get_result in Views navigation 7

Get the result of a query, as an array of etids keyed by position.

1 call to views_navigation_get_result()
_views_navigation_get_data in ./views_navigation.inc
Helper function to get the data.

File

./views_navigation.inc, line 150
Views navigation main include file.

Code

function views_navigation_get_result($cid) {
  if (!($result = views_navigation_get_cached_result($cid))) {
    if ($query = views_navigation_get_cached_query($cid)) {
      $result = [];
      $plugin = _views_navigation_get_query_plugin($query);
      $entity_type = _views_navigation_get_entity_type($query);
      $id_key = _views_navigation_get_id_key($entity_type);
      views_module_include('views');
      switch ($plugin) {
        case 'default':
          $real_query = $query
            ->query();
          $rows = $real_query
            ->execute();
          foreach ($rows as $pos => $row) {
            $result[$pos] = $row->{$id_key};
          }
          break;
        case 'search_api':
          $real_query = $query
            ->getSearchApiQuery();
          $real_query
            ->range(0, NULL);
          $response = $real_query
            ->execute();
          $rows = $response['results'];
          $pos = 0;
          foreach ($rows as $row) {
            $result[$pos] = $row['id'];
            $pos++;
          }
          break;
      }

      // Try to store the result set for a relevant time according to the view's
      // cache settings.
      switch ($query->view->display_handler->options['cache']['type']) {
        case 'none':

          // Never cache the result. We assume the view is lightweight and
          // results won't change during navigation.
          break;
        case 'time':

          // This cache could be set at a different time from the normal view's
          // one. So that this can still lead to inconsistency with fast-moving
          // views. But this is better than nothing as we have a idea on how
          // often this view changes.
          $ttl = REQUEST_TIME + $query->view->display_handler->options['cache']['results_lifespan'];
          break;
        default:

          // Unhandled cache backends. Let's store the result set during one
          // hour. This is better than never or for ever.
          $ttl = REQUEST_TIME + 3600;
      }
      if (isset($ttl)) {
        cache_set('result-' . $cid, $result, VIEWS_NAVIGATION_CACHE_BIN, $ttl);
      }
    }
  }
  return $result;
}