Quick and Dirty Links Page for Drupal
Many websites feature a page filled with links to other, mostly related websites. Generally, these links pages are divided into various categories, sometimes descending into two, three, or more levels of categorization. While Drupal is able to do this with the appropriate modules, there is no out-of-the-box method for putting all those links onto a single page.
In this article, I will demonstrate the method that I used to create a hierarchical Links page for a Drupal 5 based website. The only modules (outside of Core) that are required are Views, Links, and Links Weblink (included in the previous). This article assumes knowledge of PHP and Views creation and editing.
Setting Up
First things first. For this system to work, you'll need to download and install the Views and Links modules. Once this is done, activate them, as well as Taxonomy and Links Weblink (which came with Links). When you've done this, you'll notice that a new content type, weblink, is now available for use. It is this content type that you'll be using to populate your Links page.
The next thing you'll need to do is create a vocabulary for your links. If you've not used the Taxonomy module before, it provides a way to create categories, tags, and similar groups of organizational words and phrases. It appears as "Categories" on the Administer page.
At this time, visit the Categories page, and create a new vocabulary. Make sure to note the name that you gave it, as well as the numeric ID that Drupal assigned. You will require both of these later on in this article. Also, populate the vocabulary, by adding a few terms corresponding to the various categories you want to see for your links.
Creating the View
At this point, we're ready to create a view, for listing our links. As-is, the view we are creating will only provide the links for a single category. However, don't worry about that just yet. We'll get onto showing all the links soon enough.
Add a new view, and call it links or something similar. You'll need to remember the name for later, and it's best to keep the name of something related to its purpose. This view will be used for generating parts of a page, so open the Page section, and ensure that Provide Page View is checked.
You'll need to provide a URL, but as this view will never be referred to directly, feel free to set it to anything you want (provided it's not the same as the URL you want for your actual links page). Also, make sure that View Type is set to List. Lastly, set Pager to None, and Nodes per Page to 0.
Next, open up the Fields section. This will provide us with the content from the weblink nodes that we wish to see for each link. There are three fields you should add here: Links: Link title, Links: Link URL, and Node: Body. If you set "Show as link" for the first, you don't need Link URL (unless you want to show it off; remember, all three of these render text). Body is useful for providing a short description of the link, but isn't actually necessary.
We'll now add an argument to the view, so that when it is called upon to do its work, it will know which links to actually include. Only one argument is needed, and that's Taxonomy: Term ID. To avoid strange behaviour in case something messes up, set the Default for the argument to Not Found or Empty Text.
Now, we'll set two filters to further limit unwanted content from appearing. The first filter should almost always be used, in every view created. That's right, Node: Published. We will also set Taxonomy: Vocabulary Name. Remember the vocabulary we created earlier? This is why you had to keep track of the name. The value for this filter should be that of the created vocabulary.
Lastly, we'll add a sort, Node: Title, to ensure that the links in the category are neatly ordered. Once all of these have been done, we can save the view. And guess what? We're more than half-way complete.
Building the Links Page
Now it's time to start worrying about how each category and its links will show up on a single page. We will be using an ordinary page node to provide us with this. So create a page, and before doing anything else, set the Input format to PHP code.
We'll be using PHP here to cycle through each term in the vocabulary. As we go through them all, we will use the view created earlier to generate the HTML that displays the links. The code in this article will produce a simple links page, but with a little bit of work, any skilled Drupal webmaster can make something that looks much more attractive.
We'll start off the page by adding the <?php ?> tags, with a couple lines of whitespace inside. Next, we'll create three variables, containing the view, the vocabulary "tree", and the current depth of the vocabulary:
$view = views_get_view('links'); // the view itself
$tree = taxonomy_get_tree(1); // the web links taxonomy
$level = 3; // header tag level, 3 == <h3>
The $view variable contains the view itself, which can be reused. Rather than get the view each time, we'll be caching it ahead of time, both for cleaner code, and faster execution time. In the code above, replace links with the name of the view you created, if it's different.
Next, the $tree variable is an array of vocabulary terms. It's not an actual tree, but taxonomy_get_tree() provides all of the terms in the same order you would see them listed if you were to go back to the Categories page and examine the terms. In the code above, replace 1 with the ID Drupal assigned to your links category vocabulary.
Finally, the $level variable lets us know what HTML header to print for the category name. Since <h2> tags are generally what's used for page titles, we start at <h3> so that they don't visually conflict with the page title. So unlike the previous two, there's no point in modifying this variable.
Now that we have this setup complete, we can actually process each term and its links, via a foreach loop.
As you can see, it's pretty simple. We figure out the proper header to use based on the depth of the term, and then call upon views_build_view() to actually produce the HTML for each category of links. That's pretty much all there is to it!
Quick and Dirty Links Page for Drupal by Christopher S. Charabaruk is licensed under a Creative Commons Attribution-No Derivative Works 2.5 Canada License. Demo source code is released into the public domain.
Trackback URL for this post:
Many websites feature a page filled with links to other, mostly related websites. Generally, these links pages are divided into various categories, sometimes descending into two, three, or more levels of categorization. While Drupal is able to do this wit
Faster than you can say CMS, I've put together an article demonstrating how to add a simple yet elegant hierarchical links page to any Drupal site. All it takes are a few good modules, a bit of elbow grease, and some sites you want as links!



Re: Quick and Dirty Links Page for Drupal
Hi!
I'm trying to setup my web links page using your tutorial. But all I see are the Taxonomy vocabulary, but no links (see http://crparr.net/node/32).
What is wrong?
Thanks in advance
Re: Quick and Dirty Links Page for Drupal
What is the name of your view? And have you set it properly in the
views_get_view()call? And is it properly set up?The only part of this that's tricky is creating the view; I'd suggest double checking it, perhaps even testing it out with the IDs of the various terms within the vocabulary used for the links page.
It'd be nice if there was a simple text form that could be used for importing and exporting views. :(
--
Christopher S. 'coldacid' Charabaruk