Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As the title describes, I am trying to group the results of a view by their first letter. Googling revealed a way for me that half does the trick : display title twice, exclude from display, limit to 1(st) character, group by that. Clever I must admit but i need to reveal a grouping field even if there are no results in it such as:




Any thoughts on how to go about doing that?

share|improve this question

A simple solution would be to add a field that only had the first letter and group by that.

Views also supports doing glossary, which is a but different, but allows you to group by the first letter and count the number of results and show fx

A(2) B(2) C(0), as an exposed filter, this is done through contexts in views 3.

share|improve this answer
"A simple solution would be to add a field that only had the first letter and group by that." I actually said that in my question but seeing as "C" doesn't give me any results and I still want it presented i can't see how that helps me. I'll look into Glossary. – magtak Jan 26 '12 at 14:27
@magtak You could use theming to add missing letters – googletorp Jan 26 '12 at 14:28
could you please elaborate on that? – magtak Jan 26 '12 at 14:40
I am trying to understand "A simple solution would be to add a field that only had the first letter". Should i add a field in my content type and fill it with the letters for each node? – brainHax Aug 13 '14 at 11:19
up vote 2 down vote accepted

It was really frustrating but in the end I figured it out:

In the views row output style tpl file:

<?php print $wrapper_prefix; ?>
  <?php if (!empty($title)) : ?>
    <h3><?php print $title; ?></h3>
  <?php endif; ?>

<?php $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
foreach ($letters as $letter):?>
<div class="letter family">
  <div class="letter-header"><?php print $letter;?></div>
<?php print $list_type_prefix;  ?>
  <?php foreach ($rows as $id => $row):?>
    <?php $rest = substr($view->result[$id]->node_title,0,1);?>
  <?php if ($rest == $letter):?>
        <li class="<?php print $classes_array[$id]; ?>"><?php print $row; ?></li>
      <?php endif;?>
  <?php endforeach; ?>
<?php print $list_type_suffix; ?>
<?php endforeach;?>
<?php print $wrapper_suffix; ?>

This works for me since that view ouputs all it's results so I can safely assume $view->result will have the same elements in it as $rows. $rows afaik is the output of the current page. I am not sure if $view->result behaves in the same way.

share|improve this answer

We actually added code in the "Header" portion of our view (select input method to PHP code). Here's our php code that does what you're looking for (and it doesn't require a separate template file).

  $letters = array(
<h2>Browse by Last Name</h2> 
  <li style="display:inline; padding-right:8px;"> 
    <a href="/Full_time_Faculty_Listing" >All</a>
  <?php for ($i=0; $i<count($letters); $i++): ?> 
    <li style="display:inline; padding-right:8px;"> 
      <a href="/Full_time_Faculty_Listing/<?php print $letters[$i];?>"><?php print strtoupper($letters[$i]);?></a>
  <?php endfor; ?> 
share|improve this answer
pretty similar to my theming solution :) – magtak Jan 31 '12 at 11:09

Take a look at this article in Drupal.org. Hope it'll be useful for you, at least it helped me.


share|improve this answer
stumbled upon it during my google expedition - couldn't find anything useful for my particular issue but as i stated above i fixed it already :) – magtak Jan 27 '12 at 10:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.