Hi,

I ve found how to group by first letter a list of node titles. The trick used is to add twice the node title field, to hide one of them, to shorten it to 1 letter and then to use this field to group the results of the views in the style settings.

However I have still one problem, I have few nodes that start by a number and I don't need a new group for each number. Instead I d like to have a special group for all node which start by a number [0-9]

So I will have :
[0-9]
node 1
node 2...
A
node 3
node 4
B
node 5
node 6
...

In your opinion what is the quicker way and the most "sustainable" way to do that ?

Comments

voross’s picture

Sorry,
I have no solution for the second part, but the first paragraph, (how to set up groups) I found very useful, thanks a lot!

wrd’s picture

Hey, I think I can answer this. This is my first answer! Yay!

This is what I did in Drupal 7, but this should easily translate into Drupal 6:

1) Get the Views PHP module: http://drupal.org/project/views_php
- For Drupal 6, get Views Custom Field, which does pretty much the same thing: http://drupal.org/project/views_customfield

2) Add a new Global: PHP field to your display.

3) Use some simple code like this in your Global: PHP field:

$label = substr($row->name, 0, 1);
if (is_numeric($label)) {
    $label = '0 - 9';
}
return strtolower($label);

4) Group by that field, replacing the grouping you did with your second Title field.

merlinofchaos’s picture

Status: Active » Fixed

Answers should mark the issue fixed. :)

julma’s picture

Title: Views results grouped by first letter + a special group [0-9] » Views results grouped by first letter + a special group [0-9] + Alpha Pager made with anchors
Status: Fixed » Active

Thank you very much,

I can confirm it works well with drupal 6.

I am using exactly this code :

$label = substr($data->node_title, 0, 1);
if (is_numeric($label)) {
    $label = '0 - 9';
}
return $label;

Also useful, to find out what fields are accessible you can use:

print var_export($data, TRUE);

One last thing is that I would like to add an Alpha Pager made with anchors above the list.
An example of the final resulat is visible here :
http://www.digital-web.com/titles

Anyone knows a good solution to add this apha-pager made with anchors ?
http://drupal.org/node/649626

julma’s picture

Version: 6.x-2.9 » 6.x-2.12
esmerel’s picture

Status: Active » Fixed

The default summary view will make that kind of list

julma’s picture

Status: Fixed » Active

Sorry but I can't find a summary style that do exactly what is here :
http://www.digital-web.com/titles

Using "argument" with default summary style break the display letter by letter.

So here is a beginning of a solution :

Add the option "Rewrite the output of this field" in the Views settings of the "custom php field" and add this rewrite :
<h2 id="[phpcode]">[phpcode]</h2>

Then in the header section of the view, add :

<div>
<a href="#0-9">0-9</a> <a href="#A">A</a> <a href="#B">B <a href="#C">C</a> <a href="#D">D</a> <a href="#E">E</a> <a href="#F">F</a> <a href="#G">G</a> <a href="#H">H</a> <a href="#I">I</a> <a href="#J">J</a> <a href="#K">K</a> <a href="#L">L</a> <a href="#M">M</a> <a href="#N">N</a> <a href="#O">O</a> <a href="#P">P</a> <a href="#Q">Q</a> <a href="#R">R</a> <a href="#S">S</a> <a href="#T">T</a> <a href="#U">U</a> <a href="#V">V</a> <a href="#W">W</a> <a href="#X">X</a> <a href="#Y">Y</a> <a href="#Z">Z</a>
</div>

This is not a good generic solution because all letters of the pager are displayed even if there are no titles starting with some of them.

I hope It is possible to find a better solution.

mark trapp’s picture

I ran into this, and while #2 is a pretty interesting way to handle it, if you don't need to rewrite grouping field (for example, if you wanted to group all the numeric rows together under "0-9"), you can do this without a separate module:

  1. Create a Content: Title field.
  2. Under Rewrite Results, select Trim this field to a maximum length and set Maximum length to 1.
  3. Uncheck Add an ellipsis.
  4. Group the view by this new field. Views will group by the letter.

There are probably a few other issues that this solution doesn't handle, but it handles the simple use case.

Yuri’s picture

@ #8: Dit you actually test that? When I set it up according to the #8 method, the view will not group by the letter, since each letter is shown as a separate header, apparently views still sees each single character field as separate although it appears as the same character. So thats not working.

mark trapp’s picture

Yuri,

Yes, I did: that's why I posted it. Please note the time stamps on posts, though: I posted that 6 months ago. There's a chance something's changed since then that makes the solution unviable for your specific use-case. There are other solutions in other comments that might work better for you instead.

dave_______1’s picture

Just tested #8 and it worked perfectly!
So logical.
Don't forget to tick the 'Exclude from display' on the field to stop it showing every letter.
Works on the Taxonomy too.

Now I've just go to get a div round each group so I can float the group...

Cheers for the help Mark

EDIT: Use a HTML list and each grouping is in a UL ready to style :)

puzl’s picture

Ran into the same problem. Make sure the 'link this field to its node' is unchecked. Did the trick for me.

ndf’s picture

#8 Nice and simple

andrewtweber’s picture

#8 You're completely missing the point on grouping the numbers together...

silurius’s picture

Tip for anyone who wants to index nodes under multiple titles. (E.g., "Registering for Classes" and "How to Register for Classes" both pointing to the same node.) Link gives you a CCK field that, if set to "unlimited" can be used to create as many instances as you want. Once you've added the field and edited a few nodes, simply replace the node title field in your view with the Link title, and then group your links by first letter using the steps above.

Another tip: if your index has a large number of links, consider giving your users a means of "parachuting" directly to the link they're after by using an exposed filter. This way they don't have to search the page or scroll up and down, looking for the correct link. In my case, I added a display to my view with an exposed filter that pulls from the link title field, and on my index page (using Insert view) I positioned the exposed filter view display at the top and the full index view display just below it.

This was for D7 but should work for D6 as well.

letrotteur’s picture

Issue summary: View changes

Instead of rewriting the field, I used a custom formatter that took care of the case-sensitive issues, transliteration issues and special caracter issues.

You juste have to make sure you checked option 'Use rendered output to group rows' in the views' format settings

so in hook_field_formatter_view I build my formatter like this:

case 'vdm_bdm_first_letter':
      foreach ($items as $delta => $item) {
        $output = _text_sanitize($instance, $langcode, $item, 'value');
        $output = drupal_strtoupper($output);
        $output = transliteration_get($output);
        $output = preg_replace('![^0-9A-Za-z.-]!', '', $output);
        $output = substr($output,0,1);
        /* Handle numeric data... */
        if (is_numeric($output)) {
          $output = '[0-9]';
        }

        $element[$delta] = array('#markup' => $output);
      }
      return $element;
      break;
kienan91’s picture

#8 this work for me ! Thank you !

inman’s picture

#8 for my task, it helped. Thank!

ressa’s picture

I created a new post, since the original didn't really belong here.

riddhi.addweb’s picture

Issue tags: +alpha pager
xoruna’s picture

#2 and #4 work fine. However, I would like to know how to gather under label "0 - 9" all nodes begining by a non alphabetical character (like a "+" for example, or punctuation). Any idea?

Or if it is simplier, a way classify those nodes by the first letter they contain (thus ignoring non alphabetical characters at the begining).

ggiakoumidakis’s picture

I can't make this work on Drupal 8, the simple case, with the first letter grouping... Has anyone?

ggiakoumidakis’s picture

Finally worked, following the #12 direction, it is not clearly mentioned somewhere else, thanks.

mustanggb’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

tiagoz’s picture

#8 still works in D10, thanks!