Question

I use Drupal 7 with Commerce module and making products more info page with links to prev/next products and others pages in product category.

http://postimg.org/image/8rbcsyye3/

Products preview/announce located at category page.

It's easy to do as views page with pager and number item per page equal to 1. But how can I jump from product preview page to product more info correctly? In that case it's need to navigate to specific page number in pager. How it's possible? or may be exist more appropriate way to solve it's task.

In Pager API I found the only function pager_find_page it's not even clue to pager_set_page. Maybe because it is described somewhere somehow, but I can't find it any way.

Was it helpful?

Solution

Answer to own question. Solution for Drupal 5/6 (not sure about version)

Some other helpfull information https://drupal.stackexchange.com/questions/1561/is-it-possible-to-dynamically-set-views-pager-settings

https://drupal.stackexchange.com/questions/16046/alter-pagination-start-index

After mixing that information I wrote simple solution for Drupal 7.

When user going to product node he can jump to any other product in current category. That's why I create view with ajax enabled and contextual filter which return all node's ids from same product category by passing current page node id.

$n_id = intval(arg(1));
$nnm = db_query("select t.nid from {taxonomy_index} t left join {node} n on n.nid = t.nid where t.tid = (select t2.tid from {taxonomy_index} t2 where t2.nid = :vid) order by n.title", array(':vid' => $n_id));
$res= array();
if ($nnm->rowCount()) {
foreach ($nnm as $row) $res[]= $row->nid;
 $handler->argument= implode('+', $res);
return true;
} else {
return false;
}

At custom module for my products full information view I added hook_views_query_alter. Only in query_alter hook $view->query->where parameters are available and $view->query-pager not defined yet. It's mean that no need to rewrite default value for pager->current_page and product category's nids are avalable.

function [modulename]_views_query_alter(&$view) {
//$_GET['wtf'] -- variable to check first view load 
    if ($view->name == "view_name" && $view->current_display == 'view_display' && !isset($_GET['wtf'])) {
        //Search current node id ($view->args[0]) in all category's nodes 
        $res= array_search($view->args[0], $view->query->where[0]['conditions'][0]['value'][':node_nid']);
        if ($res) {
            //$view->set_current_page($res);
            //Set page id for pager
            $_GET['page']= $res;
        }
        $_GET['wtf']= 2;
    }
}

It is strange that there is no mention of such way to provide pager->current_page value. Unlike pager->set_current_page method it can set page value at any views hook even if $view->pager not been initialized. It sounds pretty native for me.

And thank you for correcting my mistakes, I'm sorry for my bad english.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top