Drupal 7 Excluding Node ID from URL
Share with Others
In this installment of the Daily Dose of Drupal, we are looking not at a module, but rather how to exclude a node from a view using the node/content ID. If you are new to views, you can check out our Drupal views introduction.
The video explanation will put a lot more context around exactly what I mean, but the general idea is using a view we will be able to exclude the current node id we are on (grabbed from the URL) from the view. In other words, if you are on a page about grasshoppers the view possibly on the sidebar that displays other insects won't have the grasshopper listed (ie since we are already on this page).
This little trick will greatly improve your user experience on your site. All in all, eliminating a "useless" link to the same page you are currently on is a win-win! Doesn't everyone love to win? We think yes.
Also, make sure to listen to the end for a quote that I personally feel if taken to heart can be a life changer.
Hey everybody and welcome to episode 197 of the Daily Dose of Drupal. I am Adam and today we’re going to be looking for something a little different. We’re not sure…you’re not going to be focusing on any specific module. Today we’re going to be looking at how to accomplish something in Drupal. And what we’re going to accomplish is we’re actually going to figure out how to display a view listing all of the nodes of a content type but excluding the node that we’re currently on in the view.
So that’s what we’re going to be accomplishing and I’m going to show and make more sense as we go along. But before we do that, of course, make sure you head on over to codekarate.com if you’re not already there and also make sure to check out our free sticker program we got going on. So in the corner you can click on that and we’re giving away free Code Karate stickers, trust me they’re completely free, no strings attached, we won’t charge you anything.
We ship this nice looking sticker to you if you just need to…all the thing we ask you in return is that you help us spread the word. So tweet it out to your friends and get other people to get some stickers that they can stick and spreading Code Karate word. Also while you’re at Code Karate make sure to check out all of the other things and again give us any thoughts on other modules or other things you want us to take a look at and we will do our best to get to those.
Alright, let’s get to the objective today. So what I want to do is again I want to display a block that list all of the nodes of a content type and then…but I don’t want to show the current node that I’m on. So to start, what I did is I created a content type called Blog and I added a bunch of…or just three different nodes for those blogs, so I got one, two and three in here. So if you see here I have blog test 1 and again let’s get it first to display all of the nodes of that content type. So to do that we can just simply create the easy view.
So we go to Structure, click o Views, add a new view, I’m going to call this…we’ll just call it blog post and then we want to up type content and we want to include it in just being blogs, we won’t include any of the rest of them We’re not going to create a page so we’re going to instead create a block and we’re just going to leave all the defaults fine and then you could see here below in our preview we’re seeing blog test 1, 2 and three getting displayed in all of our different blog types. So let’s go ahead and save that and I’ll just see how it looks like. So if we go back to structure blocks to get the block added, scroll down here to the view blog post, let’s add that to our first sidebar, save that and actually I’m also going to restrict that only to show on our blog content type.
So I’m going to come in here and select content types and click blog, and then if I close this you’ll see right here all my blog posts are showing up and I got one, two and three. We’ll that’s good and everything but for the goal of today it’s exactly to get blog test 1 not to show up because I’m already on that node. This doesn’t make much sense so if I go to two, two shows up which I already need a link back to the same page I’m already on so we need to get rid of that. So to do that all we need to do is edit our view and add a filter. So a filter with that is basically doing is it’s filtering the results before it gets displayed.
So to filter that I need to add a NID or the Node ID of the piece of content. So I’m going to go ahead and add that and in the Options menu here you want to do provide default value, under type we’re going to select content ID from the URL and remember in Drupal 7 content ID, Node ID same thing. So what’s that going to do is it’s going to grab the content ID or Node ID which if you look up in the bar here that’s the 10. So that’s our Node ID. So we’ll go ahead and grab that and we’re going to keep everything else default until we go to the bottom here under the more.
We want to inverse our rule so we’re going to click Exclude and what that’s basically doing is it’s flipping the rule. So if we read this here it says if selected, the numbers entered for the filter will be excluded rather than limiting the view. So what that means is it’s going tom say okay grab the 10 and if it’s the same as the Node ID we’re on which is the 10 then don’t show it. So we’re going to apply that filter and we’re going to go ahead and just save this and look and see if it works. So now you see what’s on blog test 2 or node ID 10, we don’t see blog test 2 over here because it’s the same. So excluding it, so if click on blog test 3, the three doesn’t show up on the list, blog test 1 that doesn’t show up on the list.
So again if we go back to the view just to show you, that’s how you exclude the node ID that you’re currently on. Again just adding a content filter, grabbing that ID from the URL or the node ID and then just making sure you come into the More settings and clicking the opposite or exclude checkbox. So that’s a really simple way to do this task, we don’t need to spend much more time on it but it’s definitely a handy thing, becomes in line, it’s definitely when you’re displaying blogs or similar type posts, nice way to limit that and make your user experience just that much better.
Alright so to end it today I want to leave you with a little quote, change things up a little bit. The quote is information without execution is poverty. It comes from a man named Tony Robbins and I think that’s very important in today’s world and even in with coding is, take the information that you learn from Code Karate or other resources and put that into practice. Not using information is worthless or poverty as Tony Robbins would say. So just get out there, just do whatever you think you need to do and make your dreams happen. Alright guys until next time, take it easy.