I'm creating a staff directory, with department headings and underneath each department heading, I'm using views_embed_view() from template.php to display the staff of the respective department. The way it works is that I display the department heading and when a user clicks on the heading, the view's results are revealed with Javascript.

Here's the basic PHP part of it:

$vars['page']['content']['system_main']['nodes'][arg(1)]['body'][0]['#markup'] .= 
'<div class="dept '.$class.'"><a href="#" title="'.$departments[$i].'" class="hider">'.$departments[$i].'</a>
<div class="hidden">'.views_embed_view('staff', 'block', $departments[$i]).'</div></div>';

As you can see, I'm iterating through each staff department heading and passing that into the view. This works out great and without any issues except for the pager: whenever I click on a department to toggle the view's results, clicking on the pager's "Next" button can be hit-and-miss... Sometimes it will display the next results, sometimes it will display nothing.

The view is set to display 6 items at a time (which cuts down on total load time) and this issue only happens to specific departments for some reason. So far, every department reveals proper results on the first page of the pager results, but some departments--after clicking the "Next" button--will display nothing. I originally suspected that this had to do with the Pager ID settings from the Views UI, but after adjusting it to 2 and then later to something crazy like 9999, it just didn't help at all.

Any ideas what has caused this? Possible solutions?

Insights into this would be appreciated.

Comments

nevets’s picture

While you can place multiple pagers on one page, by default they will not work because Drupal can not tell them apart. The solution is to give them all different instance values (the default is 0). Because you are only use one view though I am not sure how to vary the pager instance.

As a side note, you can avoid modify the page structure like this by using a module like eva or block reference field.

Wolf_22’s picture

I'll keep trying to research it then but so far, I'm unable to find much about overriding the pager id (assuming this is the issue I'm experiencing--which I think it is).

I keep thinking that all I need to do is find a place to override it but I'm not sure. I'll post back once I know more.

Wolf_22’s picture

I'm thinking I might be able to do this with theme_pager() somehow... It appears to open up the pager structure enough to insert different pager id values... Now I just need to figure out how to put it all together with the block view I'm embedding all over the place. :)

Wolf_22’s picture

Update: I've managed to find a way to override the Pager IDs by using the following code in my custom module (not in template.php):

function mymodulename_views_pre_view(&$view){
    if($view->name == 'myviewname' && $view->current_display == 'block'){
        $pager_options = $view->display_handler->get_option('pager');
	$view->display['default']->handler->options['pager']['options']['id'] = pager_id_generator();
	$view->display_handler->set_option('pager', $pager_options);
    }
}

function pager_id_generator(){
    static $pager_id;
    if(!isset($pager_id)){
	$pager_id = 2; //Not sure if this is necessary but I included it anyway (saw it in some example code)...
    }else{
        $pager_id++;
    }
    return $pager_id;
}

(At least, I believe this is overriding the IDs because it seemed to be in the var_dump / print_r information I wrote to a file.)

That aside, I'm still having problems with the pager's "Next" button for specific instances of the block I generate and after doing more testing, it *seems* as if it might be due to ampersands.

Here's what I'm doing in detail:

  1. I run some SQL to return an array of field values associated with user profiles. (In my case, it's an array of department names extracted from a field from user profiles.)
  2. I use a FOR loop to iterate through that array to print these off as headings for a staff listing page.
  3. In the same above FOR routine during each loop, I embed the block view and pass it the department heading value to be used as a contextual filter (so that each block prints staff corresponding to that respective department).
  4. When I view the page, everything looks great.
  5. ...but for departments that have ampersands in their department names, clicking the "Next" button in the pager I use in the view instance makes the view--minus the exposed filter--completely disappear for that respective view instance.

Here's a basic rundown of what I'm using in all this (again, from my custom module):

function mymodulename_preprocess_page(&$vars){
$bundle	   = get_bundle_type($vars);
$page_alias   = drupal_get_path_alias('node/'.arg(1));

if($bundle === 'standard_page' && $page_alias === 'myalias'){
    //flatten() is a function used to ensure single dimensional arrays...
    $departments	= flatten(db_query("SELECT DISTINCT field_department_value FROM {field_data_field_department} WHERE field_department_value IS NOT NULL ORDER BY field_department_value ASC")->fetchAll(PDO::FETCH_ASSOC));

    $vars['page']['content']['system_main']['nodes'][arg(1)]['body'][0]['#markup'] = '<div id="bottom"><form id="live-search" action="" class="styled" method="post"><label>Department Contacts</label><input type="text" class="text-input" id="filter" value="" /><span id="filter-count"></span></form>';

    $vars['page']['content']['system_main']['nodes'][arg(1)]['body'][0]['#markup'] .= '<div id="other_pager">';

    for($i=0;$i<count($departments);$i++){
        $vars['page']['content']['system_main']['nodes'][arg(1)]['body'][0]['#markup'] .= '<div class="dept"><a href="#" title="'.$departments[$i].'">'.ucwords(strtolower($departments[$i])).'</a><div>'.views_embed_view('myviewname', 'block', $departments[$i]).'</div></div>';
    }
    
    $vars['page']['content']['system_main']['nodes'][arg(1)]['body'][0]['#markup'] .= '</div>';
}

As you can see from the above code, I assign $departments the results from the query that I use to extract and flatten to a 1 dimensional array of department identifiers (i.e. - [0] => "Sales & Marketing," [1] => "Human Resources," etc.). These values come from a field found in users' profiles.

After I do this, I then iterate through each of these and print out both the heading and the block view whereby I use the department heading value as a contextual filter parameter for the view that's embedded directly underneath the headings.

Everything works great except the view instances that correspond to departments that have an ampersand (&) in them. Granted, this is somewhat an assumption that it's causing the pager issues, but it seems pretty sound given that all of the instances corresponding to those departments that have ampersands in their names are the ones having issues.

Thoughts? Does anyone know how to go about fixing this? Have I not used a proper escaping mechanism of some sort? I'm not using any validation for the view right now but when I do, it doesn't seem to help at all.

lazyD’s picture

Hey Wolf_22,

I am using views 7.x.3.14 and in my case if i use your code (hook_views_pre_view) my parent views pagination is not displayed.

Any Lead with be appreciated.

Thanks in Advance.