7

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:

A
  Alpha
  Anekin

B
  Bravo
  Borvo
C

D
  Delta
  Dooku

Any thoughts on how to go about doing that?

4 Answers 4

4

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.

4
  • "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.
    – user5005
    Commented Jan 26, 2012 at 14:27
  • 1
    @magtak You could use theming to add missing letters
    – googletorp
    Commented Jan 26, 2012 at 14:28
  • 1
    could you please elaborate on that?
    – user5005
    Commented Jan 26, 2012 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
    Commented Aug 13, 2014 at 11:19
1

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; ?>
</div>
<?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.

0

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

http://drupal.org/node/1078962

1
  • 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 :)
    – user5005
    Commented Jan 27, 2012 at 10:21
-1

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).

<?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','y','z'
  );  
?>  
<h2>Browse by Last Name</h2> 
<ul> 
  <li style="display:inline; padding-right:8px;"> 
    <a href="/Full_time_Faculty_Listing" >All</a>
  </li>
  <?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>
    </li> 
  <?php endfor; ?> 
</ul>
1
  • pretty similar to my theming solution :)
    – user5005
    Commented Jan 31, 2012 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.