I'm playing around with creating a contacts directory using CCK and Views. I wanted a set of tabs that split the nodes up by the first letter of the "surname" text field.
I managed to do this by creating 26 separate views, all as tabs, and changing the filter letter each time. This took a long time! But it does work ok - the URLs are view/contacts/a, view/contacts/b etc.

Is there a simpler way of achieving the same thing? I'm thinking that arguments would be a better way of doing this but I don't know what PHP code to use to process the argument.

Comments

karens’s picture

Status: Active » Fixed

There's a very easy way to do it. Go to your view. Open up the 'Arguments' section. Select 'Node: Title'. Give it a name like 'By Title'. Put the number '1' in options (that means it's looking for 1 letter). Choose a summary, if you like, probably 'Summary, sorted'.

Now go back and make the url the part of the url BEFORE the letters, i.e. view/contacts instead of view/contacts/a. Remove whatever filter you added to limit the view by the letter because the argument will do it.

Save the view.

Now go to view/contacts and you will see a list of each letter that has any values with the number of values for that letter. Select any one of them to see the view filtered by that letter. You can link to the main page (view/contacts) to let someone pick a letter, or link directly to view/contacts/a to take them right to the view filtered by that letter.

talltim’s picture

Thanks for that - I've done what you suggested and it's been useful to get to understand (yet!) anothe feature of views.
I do have a slight problem in that although an argument of the Node Title is able to have an option of "1" - i.e. sorted by the first letter of the title - this option isn't available for other text fields (such as "surname", which is the important one in this case).

My guess is that CCK doesn't expose text fields as being available for the options of an argument? If that is the case, is it related to this patch: http://drupal.org/node/60384 which providers more options for filters.

yched’s picture

In a way, it is related. It involves arguments instead of filters, though (as you pointed out).

Clearly the current support of views arguments in cck doesn't allow 'option' for arguments'.
You should probably fill in a separate feature request for this, as arguments and filters are dealt with differently.

As a side comment :
I wasn't aware that arguments could define an 'option'. It's not clear to me where to find an exhaustive Views API featureset. Something like : allowed keys in fields, filters, sorts and arguments array.
The one in http://drupal.org/node/42609 is outdated.

karens’s picture

I don't think there is any exhaustive list of features. I know merlin has said he wants to get his documentation updated. What I've been doing is looking through the views.module and the .inc files in the views/modules directory for ideas on what kinds of things can be done. For instance, I found this alphabet trick by looking at the pre-defined arguments in node.inc. What we would have to do in cck is copy the views_handler_arg_nodeletter function from node.inc and adapt it to the cck table and field names.

Anonymous’s picture

Status: Fixed » Closed (fixed)