Views Arguments

On this episode, Bob talks about Views arguments.  For a non-developer, arguments get really confusing really quickly.  By showing two examples of how to create pages and blocks using arguments, Bob hopes that someone out there isn't confused and learns a little something about the power they have to offer.

 I put together a feature where I used arguments in views somewhat heavily. If anyone wants to see views putting this stuff in action, check out http://projects.josiahritchie.com/project/board-game-sign. It's just simply a drupal feature that is designed around the idea of providing table-top game sign-ups, but uses arguments to provide blocks to help users know where certain games are being played or what games are being played at what events. It's lame, but might demonstrate some of this stuff if you're more of a hands on learner. (or learn by breaking things and then rebuilding them. :-)

A while back I started writing a module to do a photo-a-day display my website but I kept thinking I should be able to do it with arguments and views. Your video reinforces my thoughts. As a photographer I wanted to have a section of my website display photographs sort of like a photo-a-day type of thing. I created a  content type specific to just showing a photo and a caption and used image cache to create the full node image and a thumbnail square image.

On the photo-a-day node page I wanted a single photo displayed big and then say 10 thumbnails photos underneath to show the 5 thumbnail photos before the current day's photo being viewed and the 5 thumbnail photos displayed after the current day's photo  being viewed. These photos would essentially be the way to move around between dates to see different nodes. This thumbnail navigation sounds like it could be easily accomplished with views arguments to only show photos of this content type of the thumbnail variety. I get how to do that. Create 2 blocks filter both to an argument based on the node id of the node limiting it to the content type being viewed with one block showing only the thumbnail with link to the next 5 ascending and the other the next 5 descending.

Problem is I wanted to step it up a notch.

I'd like to always display 10 photos (assuming there are more than 10 nodes of this content type)  so even if the photo they are looking at is the current day's photo or the very first photo it would always display 10 photos. Is it possible to do this with arguments? I'm thinking no because I would need to know the number of thumbnails to be displayed in each view. If there were only 3 thumbnails to display in one block view it would have to compensate and display 7 on the other block.

Possible? Any thoughts?

Im biased as a developer but it shouldn't be that hard to get.

Exposed Filters and arguments are very similar. Maybe its good to think a bit on that.

If you create a filter you have the option to expose it which means that the user will get a drop down with all the possible values. In Bobs example there will be a drop down that lits the options bacon&cheese. (This way you don't have to set up a view per taxonomy term either, so this feature is not exclusive to arguments).

Think of arguments as a replacement for dropdown. Instead of the user filtering on a taxonomy, other pages that link to the view can instuct the view what to show. The most common way to instruct it is through the url what Bob showed.

Thats the dynamic aspect of it : the page with the view on is not static ie. it will not always show the same thing. It will show things by looking at the url.

Think of exposed filters as a way to let users instruct the view.
Think of arguments as way to propagete a choice of the user to other pages. You let the code/machine filter. If a user chose magazine 5 in a view of all magazines she will have clicked on a link that passed the argument of 5 (propagating a choice) to a view that shows all articles belonging to magazine 5.

You are right that the term argument is also used in what devs call a function :) Lets say we have a function (replace function with task if that makes more sense) that sends out an email to a user: the sendmail function.

If the email is hardcoded ( a term used to indicate that we simply spell out the email as text) inside the function its pretty useless. But if we give the function an argument that takes an email addrees then the function can do what it's supposed to... send out to emails to any user.
We just give the function the email and that email will be used by the function. In php this will be something like the following

sendmail(){
   phpmail(bob@mustardseed.com)
}

the code above is useless... we want to send an email to anyone :

sendmail(emailaddress){
   phpmail(emailddress)
}

=> emailaddress is an argument and we can call the function like so : sendmail(dries@drupal.org)

Some other ways to think about it :

- parametrizing a view
- injecting a value in to a view
- filtering a view by the url

 

 

 


 

"If a user chose magazine 5 in a view of all magazines she will have clicked on a link that passed the argument of 5 (propagating a choice) to a view that shows all articles belonging to magazine 5."

lets propagate some more :) Our users sees a list of all the articles belonging to a given magazine.

We want to user to have the ability to click on an article and show a list of articles that reference that article (in their footnotes for example.) What do we do?

We link to the a page that has a "referenced articles view" on it that takes as an argument an article.

Now we are truely creating dynamic websites: If a new magazine is published we don't have to change any view because all pages are parametrized (they listen to the url/they take an argument). We are now propagating choices trough the use of links.

Let me know if you have some more questions because I'm getting abstract. I know that developers like to do that :)

 

Hi Bob - so glad your back! You have taught me more than you can possibly imagine!

I was thinking about how I might set up arguments to enforce a "call to action". i.e I would like a user to click on an image and for that image to pass an argument (kind of burying the argument in the image).

Using views I have figured out how to hard code an image into a view using:

<a href="yourlinkhere"><img src="<?php print base_path().path_to_theme(); ?>/images/image.png" width="200" height="199" /></a>

However, I have a feeling this is not "best practice" - is there a better solution?

I was thinking about perhaps setting up a content type called "Call to Action" with a CCK image-field. Give it a description field that includes the link - but that seems wrong too... as its pretty much hard coded again. 

Any hints or tips would be appreciated from anyone... buckley???

 

 

Sorry just to add... the link would be to a view that has already been created and the clicking the image would provide the argument for tha view - but I am not sure how to "embed the argument into the image"... i hope you understand my waffle!

Great podcast Bob !

Just chiped in some bucks ;-)  Not that much for now, but i'm sure this wasn't the last one !

It was very clear for me with your examples..

 

Greets from germany, and keep it up !

Hi rustyleaf,

Im nut sure I understand your question. I think you want to place information about a specific call for action in a list/view. This is possible with "Replacement patterns" which you can specify when adding/uptdateing a field.

Maybe its better to tell what you want without working out a solution with drupal/arguments so that we are focused only at the problem. I have the same username of buckley on drupal so if you want to contact me go ahead and I will help you  out

Thanks so much Bob, this helped me greatly!

You are great POP; I really love your tutorial. It helped me alot.

I hope one day you will explain how you did  this slideshow of the image in this website [ http://stjohnswestbend.org/ ]