Hi,
I'm trying to get an alpha pager working with Views 2. I've done a lot of searching for support on this and I think I'm close, but no cigar yet.

I'm a novice w/ views, so I suspect I'm missing some obvious step and would greatly appreciate a nudge in the right direction.

I set up a view with 2 displays, one page and one attachment, sorted by node:title. The page view looks just fine.
On the attachment view, I created an argument on node:title, set to override…...
Action to take if argument not present: summary sorted ascending.
Glossary mode on
Character limit 1
Case:upper case
Case in Path: lower case

Then on the next screen,
Separator ‘ | ’
inline
no formating

Now I get a nice alpha pager at the top of my page.
But it doesn’t do anything. When I click a letter, the page refreshes but doesn't move from the first page of the view.
I tried adding %1 to the argument title, but that didn’t change anything.

Any suggestions?

Thanks.

Comments

WorldFallz’s picture

Views 2 comes with a glossary type alpha pager view-- just enable it and clone it.

_
Care about the future of the Drupal.org forums? Please join our conversation and show support for improving the forums infrastructure.

vonn.new’s picture

Thanks! I was so deep into the views page I didn't see the forest through the trees.

WorldFallz’s picture

you're welcome-- it's easy enough to get lost in all those options with views2, but that's part of what makes it great!

_
Care about the future of the Drupal.org forums? Please join our conversation and show support for improving the forums infrastructure.

georgekosmides’s picture

This is just what I need. But I literally have searched everywhere in Views 2 and cannot find how to enable the glossary view style for an alpha pager. Can someone tell me explicitly where it is? Thanks

WorldFallz’s picture

In the list of fields at admin/build/views should be a view named "glossary"-- just click the 'enable' link.

_
Care about the future of the Drupal.org forums? Please join our conversation and show support for improving the forums infrastructure.

budda’s picture

Any thoughts on how to get a full A-Z to display, even if there is no results for a given letter?

I can't seem to find a suitable theme function to override to produce the right output. Views2 certainly has a (useful) maze of template possibilities now!

malc_b’s picture

This will do it in a custom module then add php code to the view header to call it, or you can add the function code to each view header.

<?php
function mymodule_alphapager($apath) {
 
$attributes = array( 'class' => 'alpha-page' );
 
$output = "";
 
$output .= "<div class='alpha-pager'><div class='alpha-pager-all'>";
 
$output .= l('all', $apath . '/all', $attributes);
 
$output .= "</div><div class='alpha-pager-letter'>";
  foreach(
range('a', 'z') as $letter) {
   
$output .= l($letter, $apath . '/' . $letter, $attributes);
  }
 
$output .= "</div></div>";
  return
$output;
}
?>
malc_b’s picture

I've another question though. Is there a way to get an "all" link other than hard coding that into the attachment header and then using css to get the position inline with the rest of the pager?

rkesiar’s picture

It took me a while until I managed to figure out how to create the "all" link using the view's attachment header and CSS. This may save some time for others trying to do the same. It is based on the stock "Glossary" view. The input format of the attachment header has to support the tags used and automatic line break should be disabled to let this work. The main variable is the name of the view which is "glossary" in this case.

The CSS part could also be added to the theme's style.css file, but I think it is better to add it directly to the view so everything else can stay unchanged.

It wold be very nice if the Views module would support adding the "All" link and hacks like this wouldn't be neccessary.

<style type="text/css">
div.view-glossary div.view-header {
  float: left;
}
</style>
<span class="views-summary views-summary-unformatted"> 
  <a href="/glossary/all">All</a>  |&nbsp;
</span>
isaac77’s picture

These may prove useful if you are trying to build an alpha page:

http://tedserbinski.com/tags/drupal/creating-alpha-pager-with-views-2-an...
http://drupaleasy.com/blogs/ultimike/2009/02/using-display-attachments-p...

I have the same question as malc_b (below): how to append an "All" link to an attachment used as an alpha pager. Anyone?

iamjon’s picture

I know the original question has been answered, but I achieved a similar result via the template.
From the views module copy views-view-summary-unformatted.tpl.php to your theme folder.
Replace the code with this:

<?php
//Create an array with the alphabet.
$letters = range ('a', 'z');

foreach(
$rows as $row){
 
//Create an array of the existing letters, from the view output
 
$existing_letters[] = $row->title_truncated
}
//Create container div
print "<div class='view-Glossary'>";
//Loop over letters in the alphabet, then check if they exist in the views output.
foreach($letters as $letter){
 
//If they exist, create a span with a link
 
if(in_array($letter, $exist_letters)){
    print
"<span class='views-summary-link'><a href='/glossary/$letter'>$letter</a> |</span> ";
  }
  else {
   
//If they don't exist add the letter
   
print "<span class='views-summary-unllinked'>$letter |</span> ";
  }
  
}
print
"</div>";
?>
mistermarco’s picture

Thanks iamjon. Your code helped me a lot. I found a typo in it, though. This line:

if(in_array($letter, $exist_letters)){

should read:

if(in_array($letter, $existing_letters)){

Also, this particular method shows a pipe ( | ) after the letter Z. Our designer didn't like that, so we came up with this code:

<?php
 
//If they exist, create a span with a link
 
if(in_array($letter, $existing_letters)){
   
$nav[] = "<span class='views-summary-link'><a href='/glossary/$letter'>$letter</a></span>";
  }
  else {
   
//If they don't exist add the letter
   
$nav[] = "<span class='views-summary-unllinked'>$letter</span>";
  }
}
print
implode(" | ", $nav);
?>

Thanks,

- marco

menapia’s picture

There was a recent update and the current stable version of views-view-summary-unformatted.tpl.php (to v. 1.2.4.1.) I edited above to reflect new environment.

This worked for me but you may have to tweak it to make it to meet your needs:

As above copy the views-view-summary-unformatted.tpl.php file from the Views module into you theme director. Replace the code with this code:

<?php
//Create an array with the alphabet.
$letters = range ('a', 'z');

foreach(
$rows as $id => $row){
 
//Create an array of the existing letters, from the view output
 
$existing_letters[] = $row->link;
 
//Keep track of urls.
 
$urls[$row->link] = $row->url;
}
//Create container div
print "<div class='views-summary-unformatted'>";
//Loop over letters in the alphabet, then check if they exist in the views output.
foreach($letters as $letter){
 
//If they exist, create a span with a link
 
if(in_array($letter, $existing_letters)){
   
$nav[] = "<span class=views-summary-link><a href=" . $urls[$letter] . ">" . $letter . "</a></span>";
  }
  else {
   
//If they don't exist add the letter
   
$nav[] = "<span class='views-summary-nolink'>$letter</span>";
  }
}
print
implode(" | ", $nav);
print
"</div>";
?>
liquidcms’s picture

and adding this after the first foreach will add the ALL index at the end of the alpha index

<?php
//add ALL at end
$urls['ALL'] = str_replace(strtolower($row->link), "all", $row->url);
$letters[] = "ALL";
$existing_letters[] = "ALL";
?>
jfox77’s picture

This was driving me nuts for days and I finally figured it out!

I have both a block and an attachment display on my view. Both have an argument of Node: Title but I set the argument settings a bit different for the 2 displays.

For the BLOCK display, I have an argument of "Node: Title"
** Action to take if argument is not present: Display all values
Check Glossary Mode, with character limit of 1

For the ATTACHMENT display, I have an argument of "Node: Title"
** Action to take if argument is not present: Summary, sort ascending
Check Glossary Mode, with character limit of 1

** The main trick here is using "Override" on the attachment display so that it's the only one using the "Summary, sort ascending" option.

WorldFallz’s picture

Here's a complete version that preserves the node count, adds the "ALL" option, uses capitals, allows counts to be styled separately, and cleans up the quotes:

<?php
$total
= 0;
$letters = range ('A', 'Z');
foreach(
$rows as $id => $row){
 
$existing_letters[] = $row->link;
 
$urls[$row->link] = $row->url;
 
$counts[$row->link] = $row->count;
 
$total += $row->count;
}
$letters[] = 'ALL';
$existing_letters[] = 'ALL';
$urls['ALL'] = substr_replace(strtolower($rows[0]->url), 'all', -1, strlen($rows[0]->link));
$counts['ALL'] = $total;

print
'<div class="views-summary views-summary-unformatted">';
foreach(
$letters as $letter){
  if(
in_array($letter, $existing_letters)){
   
$nav[] = '<span class="result"><a href=' . $urls[$letter] . '>' . $letter . '</a></span><span class="count">(' . $counts[$letter]. ')</span>';
  }
  else {
   
$nav[] = '<span class="no-result">' . $letter . '</span>';
  }
}
print
implode(' | ', $nav);
print
'</div>';
?>

Confirmed to work in views 7.x-3.x -- enjoy.

_
Care about the future of the Drupal.org forums? Please join our conversation and show support for improving the forums infrastructure.

inteja’s picture

This would be perfect if it also prepended a "0-9" link that returns all node titles beginning with a numeral, and provided an 'active' class for the current page :-)

I've managed to add the numerals separately i.e. 0 | 1 | 2 | 3 ... A | B | C ... ALL

But it would be better as 0-9 | A | B | C ... ALL

Any ideas on that?

--
Brian.

WorldFallz’s picture

Yeah-- I thought the same thing, but no, I never did get it working and ran out of time for trying.

_
Care about the future of the Drupal.org forums? Please join our conversation and show support for improving the forums infrastructure.

Stew-bee’s picture

Where does this code go? Does it go in views-view-summary-unformatted.tpl.php

WorldFallz’s picture

yep.

_
Care about the future of the Drupal.org forums? Please join our conversation and show support for improving the forums infrastructure.

bensey’s picture

I found the ALL link breaks when using exposed filters. This version also preserves the queries from exposed filters, and re-adds them to the end of the "ALL" url.
There might be a better way to do this, I'm not a PHP guru.

<?php
$total
= 0;
$letters = range ('A', 'Z');
foreach(
$rows as $id => $row){
 
$existing_letters[] = $row->link;
 
$urls[$row->link] = strtolower($row->url);
 
$counts[$row->link] = $row->count;
 
$total += $row->count;
}
$letters[] = 'ALL';
$existing_letters[] = 'ALL';

$url_query = explode('?', $rows[0]->url);

$urls['ALL'] = substr_replace(strtolower($url_query[0]), 'all', -1, strlen($rows[0]->link));
unset (
$url_query[0]);
if (isset(
$url_query)) { $urls['ALL'] .= '?' . implode('?', $url_query); }
$counts['ALL'] = $total;

print

'<div class="views-summary views-summary-unformatted">';
foreach(
$letters as $letter){
  if(
in_array($letter, $existing_letters)){
   
$nav[] = '<span class="result"><a href=' . $urls[$letter] . '>' . $letter . '</a>'; // Uncomment for counts .'</span><span class="count">(' . $counts[$letter]. ')</span>';
 
}
  else {
   
$nav[] = '<span class="no-result">' . $letter . '</span>';
  }
}
print
implode(' | ', $nav);
print
'</div>';
?>
griz’s picture

Also, if you want your alpha pager links to validate you have to get rid of the empty arguments (assuming you have exposed filters).
The first regex finds any number of "word" chars (includes underscores), an equals and an ampersand.
The second finds an ampersand followed by "word" chars, and then an equals at the end of the string.

<?php $row->url = preg_replace('/[\w]*=&/' , '' , $row->url);
      $row->url = preg_replace('/&[\w]*=$/' , '' , $row->url);
      $row->url = preg_replace('/\?$/' , '' , $row->url);

And the third cleans up the ?s left at the end.

I'm using the built-in alpha pager on D6 but I think this would work on $urls['letter'].

bdone’s picture

yet another modified version, adding 0-9, special characters, and ALL.

<?php
  $total
= 0;
 
$letters = range('A', 'Z');
 
$letters = array_merge($letters, range('0', '9'));
 
$letters = array_merge($letters, range('!', '/'));
  foreach (
$rows as $id => $row) {
   
$existing_letters[] = $row->link;
   
$urls[$row->link] = $row->url;
   
$counts[$row->link] = $row->count;
   
$total += $row->count;
  }
 
$letters[] = 'ALL';
 
$existing_letters[] = 'ALL';
 
$urls['ALL'] = '/artists/all';
 
$counts['ALL'] = $total;
  print
'<div class="views-summary views-summary-unformatted">';
  foreach (
$letters as $letter) {
   
$active_class = '';
    if (
arg(1) == drupal_strtolower($letter)) {
     
$active_class = 'active';
    }
    if (
in_array($letter, $existing_letters)) {
     
$nav[] = '<span class="result"><a href=' . $urls[$letter] . ' class=" ' . $active_class . '">' . $letter . '</a></span>';
    }
    else {
     
$nav[] = '<span class="no-result" class=" ' . $active_class . '">' . $letter . '</span>';
    }
  }
  print
implode(' ', $nav);
  print
'</div>';
?>
Sylense’s picture

Does the code above apply to D6? I tried using it on D7 and got an error message and the summary printed all of the special characters as opposed to a hyperlinked 0-9 or #

marinex’s picture

@griz Hi I would like ask you, where can I put your improvements? I create template of summary and I put snippet from "ALL option breaks with exposed filters".

Thanks M.

fatallozenge’s picture

This is a mash-up of a lot of the above code, but was the only way I could get it working on my site. This shows all letters even if they have no nodes, node counts, and has a working all link. Produces no errors when used in conjunction with exposed filters.

<?php
$total
= 0;
$letters = range ('A', 'Z');
foreach(
$rows as $id => $row){
 
$existing_letters[] = $row->link;
 
$urls[$row->link] = $row->url;
 
$counts[$row->link] = $row->count;
 
$total += $row->count;
}
//add ALL at end
$letters[] = "ALL";
$existing_letters[] = "ALL";
$urls['ALL'] = str_replace(strtolower($row->link), "all", $row->url);
$counts['ALL'] = $total;

print
'<div class="views-summary views-summary-unformatted">';
foreach(
$letters as $letter){
  if(
in_array($letter, $existing_letters)){
   
$nav[] = '<span class="result"><a href=' . $urls[$letter] . '>' . $letter . '</a></span><span class="count">(' . $counts[$letter]. ')</span>';
  }
  else {
   
$nav[] = '<span class="no-result">' . $letter . '</span>';
  }
}
print
implode(' | ', $nav);
print
'</div>';
?>
Grentastic’s picture

It got me pointed in the right direction, but I had to change one line:

<?php
$urls
['ALL'] = str_replace(strtolower($row->link), "all", $row->url);
?>

It did not account for the possibility of URLs longer than just the letter. For example: "/sample/a" would be changed to "/sallmple/all".

The following seemed to do the trick:

<?php
$urlPieces
= explode("/", $row->url);
$urlPieces[count($urlPieces)-1] = "all";
$urls['ALL'] = implode("/", $urlPieces);
?>
gottaknow’s picture

A little confused. Cloning the glossary is in a table format not unformatted. The choice of themes in view display output is views-view--glossary-xxx.tpl.php. When I add the code to this file and scan it, the file is recognized. However on the page I only get the A|B|C with no details and no links.
Remove the file, rescan views, I get the glossary view back without all A|B|C and ALL.
Not sure why the file doesn't seem to pick up the information on the selected content type filter.
Using Drupal 7 Views 3
Any ideas?