Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now we are going to show a method that makes Views even more dynamic by allowing the user to provide input to the View, usually at the end of the URL. The method uses something that called in Drupal speak, Arguments. An understanding of them opens your data display possibilities up enormously. The system of arguments is conceptually tricky and the possibilities through it are far too numerous to cover in depth here but here is a taste. If we go to http://localhost/node/3, we see whatever node three is. That last part, the three is the argument and the argument is being passed to Drupal through the URL after this little bit node.
Here is another example. If you go to Administer > User management and Users, you see list of all of the users on your system. Click on any one of them and it goes to the page for that user, which is localhost or whatever your domain name is, /user/6. Once again, the 6 is the argument that's being sent to Drupal. If we change that 6 to 5, we would see user number 5 and so forth. In computer science terms, a value that changes is called a variable and the value that feed into it is called an argument. You can write out how arguments work by using a placeholder. In our case, we are going to use the Percent sign as our placeholder. So as you saw, the Nodes are viewed by using /node/placeholder and Edit would be /node/ placeholder/edit. Same is true for Users.
So you may ask, what use is such a system of arguments to you and how can you implement them? Let's step back a second and take a look at how we are showing off the people on our site right now. Let's pretend that we didn't want to show everybody in the site, but instead only wanted to show those people whose name you already knew. Well, you could do that by forcing people to type in that name as an argument. I'll show you how by setting up a view. Go to Administer > Site building and Views and we are going to add one.
We'll call this View argument_directory and the view description will be Argument directory. As before, it's a view of the Node type. Scroll to the bottom and click Next. First, we'll just add a Field, say the node title, just to give us something to work with. Scroll down here, filter by Node and find Node: Title, add it and do our usual business where we take away the Label and Link the field to its node. Update and we see everything as we usually do. We are going to filter that out so we only see those nodes that have been published and that are of the Person content type.
Once again, we select the Node group, roll down, find Published and Type and add those. We want only those that have been published and that are of the Person type and Update. There, we now have our list. I'm going to add one more piece of information just to demonstrate how sensitive information might be shown otherwise. I'll make it the date of birth. Scroll down and you'll remember since it's a CCK field, it will be under Content and there it is. Content: Date of birth, add it, scroll down a little bit and Update, there.
Now we have the names and the Date of birth. Now comes the tricky part, where we'll only show Eli de Nada's date of birth if somebody enters the name Eli de Nada. We go back up to Arguments and add one, scroll down, the argument is going to be based on the Node: Title, so we'll scroll down and find that and add it. Now once we click Add, we'll be confronted with a lot of options. Don't worry. We are going to go through a few of the more important ones. We won't touch all of them but when you are done, you will understand well enough how this works.
Click on Add. There are options. Let's scroll down and see what happens if you just leave it in its default form. Click Update and then go back down and take a look at our preview. Well, nothing actually happened. But let's try typing in Tom de Nada. This live preview area has a space where you can enter arguments to test things out and that's exactly what we are doing. We'll preview it and we see only Tom de Nada. So it is actually working, except we are also seeing everything when no argument is entered.
Fortunately, there is a way to fix that. We go back up here and click on the Argument again. One of the settings is Action to take if argument is not present. In our case, we'll Display empty text. Scroll down, click Update and let's see how that works. Aha! Now we see nothing unless we enter someone's name. Let's try Dani de Nada and Preview. Very good! It's actually working exactly as we want. Now how does this actually come to play? Well, if we go back up here and Add a Page and let's add a path to that, say hidden, and Update. Now we are still using all of our default values including that default Argument. Let's save it and go to that page, hidden.
Now we see nothing, right? But if we add a slash to the end of it and then add say Tom de Nada, now we see the record. This is exactly what we originally wanted to happen, right? I'll click on Edit to go back to that view. There is still a security hole in our Argument however and I'll show you by clicking on the Argument and scrolling down to look at the options. It's in this Wildcard. Right now if we type in someone's name, as you saw, we see just that person's name and if we type in nothing, we see nothing. However, if we type in that Wildcard value, which is All, we see everybody.
Let's go back up and edit that Argument and remove the Wildcard. Scroll down some more, click Update and let's try it. As you can see, the All argument now doesn't do anything. I'd like to show you one other use of arguments and this is to override the way that Drupal works when it shows you a node. Right now if we go to localhost/node/3, we actually see a node, just as we would expect. However, for the way that we are now building our website, we don't want people to be able to directly enter in those node numbers. So we are going to create a new View, which overrides that. This is an important point. Views will actually override what Drupal does naturally in its core installation, which gives you a lot of control and let's you add a lot of security and other features.
We will go to Administer > Site building and Views and once again, we are going to add one. This one will be called override_nodes and again the description, Override nodes. It's going to be a View of Nodes again. Click on Next. In this case, we want to give the user unhelpful information. In other words, when they try to go to that Node, instead of getting the node that they want, they will get something saying "sorry you can do that." To do that, we'll add a field and you may remember that there is a global field where you can enter pretty much any text you want. We'll add that, get rid of the Label and add the text "I'm sorry, you can't do that." Scroll down and click Update.
Right now we are seeing that text one time for every node that exists in the site. However, we'll change that with Arguments. Go up to Arguments and add one. Now the part that we are trying to override is the Node id. You remember it's localhost/node/ and then some number. That number is the way Drupal internally identifies the different nodes and it's called the node id. Scroll down, go into the node group and select Node: Nid and Add and there is our Argument.
Once again, we want it to Display empty text if nothing is given as an argument and we want there to be no Wildcards. Delete that, scroll down a bit more and Update. So what happens now if our Argument is Node 3? We get that message, 'I'm sorry, you can't do that.' What if we have no Argument? We get empty text. What if we say All? We get nothing. Once again, we get empty text. So how can we use this? You remember that people are going to see those nodes on specific pages.
The way we can actually put these arguments to use is by adding a page and setting its Path to be node followed by that argument. Its placeholder once again in Drupal is the Percent sign and that's explained right here underneath the field. In fact, you can have multiple arguments and separate them out by %1, %2, in many places in the interface. We'll leave it like this and click Update. Now let's save it and try to go to that page, node/3. We go to localhost/node/3 and we get exactly what we expected. 'I'm sorry, you can't do that.' Because we are the administrator, we are also allowed to edit the node, but you get the idea, we can also add another Argument for node/argument/edit and go on from there. I think you can now start to see the power of using arguments, right? In fact, for the purposes of our videos, we don't really want to restrict the access quite so much. So we are going to get rid of those two Views that we just created.
Of course, we go to Administer > Site building and Views and then it's very easy to simply find that hidden and delete it and the override_nodes and delete it. As I said earlier, Arguments are a big subject and these are just a few things you could do with them. If you want to go further, certainly poke around the built-in views after being sure you know how to revert them to normal. The video "Learning from Built-in Views" shows you how. There are several other resources, which will also help you learn about arguments. One of them is Learning from Built-in Views, as I mentioned, and the other one is the site drupaleasy, which has several articles on the subject.
Further more as always, you can find a lot more information on the drupal.org website.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.