Welcome to the Geeks & God Static Archive. Read more »

Creating a Simple Link List with CCK and Views

User Rating:
4.09524
Average: 4.1 (21 votes)

Tutorial Images

Creating a Simple Link List with CCK and ViewsCreating a Simple Link List with CCK and ViewsCreating a Simple Link List with CCK and ViewsCreating a Simple Link List with CCK and ViewsCreating a Simple Link List with CCK and ViewsCreating a Simple Link List with CCK and Views

Old school web design meant including a page full of links to other websites. As older sites are transitioned into current practice, many site owners expect this trend to continue. And in some ways it has, most commonly as a block containing a blogroll of other sites. This method can provide both solutions by creating a custom content type that will turn links into nodes, and can then display them as a page or block using Views. This tutorial assumes that you have a running Drupal site and understand the basics of downloading and installing contributed modules.

For this exercise, we'll need the CCK, Views, Link, Automatic Nodetitles and Token (Thanks, kswan!) modules. Unpack each module into its own folder under sites/all/modules, then enable Content, Links, Automatic Nodetitles, Token, Views and Views UI on the admin/build/modules page.

Creating the Content Type

Custom content types aren't as difficult to create as it may seem. There is nothing special about the delivered Story and Page types, which can be altered, renamed or removed, depending on how you want your site to function. Any new content type will initially have the same properties as these default types. Under admin/content/types, simply select Add Content Type to get started. The name of our new type will be Link, and the type will be link. For my example, I've relabeled the Body field as Description, allowing the user to use this field for optional teaser text. Also, we won't want these nodes automatically promoted to the front page, and we'll disallow comments and attachments.

With our basic link type created, we're now ready to add a Link-type field. I made mine with a label of "Link" as a required field with the link title required. We need this, because we're also going to force each entry to use the link title as the node title, rather than make the user enter the information twice.

We'll want to fine tune the display of our custom field, making the label hidden, and the field display "Title, as link" for both teasers and full entries.

Now we'll go back to edit the content type itself and spin open the Automatic Nodetitle section at the top, setting title generation to "Automatically generate the title and hide the title field." Assuming that we've created a field named link, our title pattern becomes [field_link-title]. We've now created a fairly simple content type. We'll simplify the user's job of data entry even more before we're through.

Create a View

For this example, we're going to build a simple block view and associate it with a single page. This will make it easy for future site maintainers to edit the info at the top of the page without delving into the depths of views editing. Create a new view and provide a block, making a list view. Because of the way we defined our view, we can use the full text of the node body as the only field to display. This will show the our links, followed by the optional descriptions in the body text if our users provide them.

Set filters on the feed to only display published nodes of type link. Sort the view however you prefer.

Now create a regular page node titled Links or Link List. For best results, enable the Path module and give this page a simple URL of "links." This way, you'll be able to use the URL alias when setting up the block. In the blocks menu, display your new block in the content area, and then go into the "configure" link and set the block to only appear on the links page.

That's all there is to it! Now you can allow site users to create links without them having to actually manage the presentation aspects of the links page. The only side effect of this method is that the individual link nodes are not easy to access for editing. This can be fixed by providing your users with a table view that includes edit links.

Update for Drupal 6

This tutorial was originally written for Drupal 5. Setting up CCK in Drupal 6 isn't that different, but the Views configuration has changed a lot! However, the choices made to display the view aren't really much different. Here's an example of how the same view would be configured in Drupal 6.

Note that the Style is HTML List, the Row Style is Fields, and the only fields included are the link field and the node body. The body is still an optional description, so it won't always be displayed. Also, the filters are essentially still set up the same way, displaying only published nodes and the desired content type.

Nice tutorial. I have a

4

Nice tutorial. I have a couple thoughts.

1. I think the Token module will be necessary for the autonodetitle to work with the title pattern [field_link-title].

2. Of course the page and URL could be created in Views without a page node and Path module. Also, creating the page in Views you could show more detailed content on the page than the block.

Thank you.

Thanks for the feedback

Kevin,

Thanks for the feedback, and a good catch on the Token Module being required for this. I'll update the original to include it.

The amount of detail shown in this example was pretty much by choice. It should work the same way as a page or block view. In either case, the desired result was to show only the links and their descriptions. In this case, the view is the intended result; I wouldn't really want the users to look at the individual nodes that make up the list. Not that there would be any harm in that, but it's not how I would want to display the data in this case.

The idea for attaching a block view to a page node actually came from Rob Feature, and here's the reason for it. When we set up sites for end users, they like to be able to edit the text at the top of pages like this, but probably won't want to delve into the guts of views editing to do it. By presenting the view this way, users can be given permission to edit page nodes without having access to the views editor.

This can also lead to some pretty fun applications when using Views in Drupal 6. For example, by embedding two views on one page, you can have the first view show a single node as a full node or a teaser, then show the rest of your nodes as a table or list view.

Micah

Thank You

5

Micah,

I ended up using this for a production site. My need was this:

Migrating from a static html site to Drupal, I wanted the "in the news" links page to be a link-node page, where the end user could easily add a link node, have it show up on the page, and push out via an RSS feed for a Twitter account.

Your tutorial was fantastic and helped me accomplish my goal. Good work.

Dean P. Simmer
Youth minister, barista and IT guy for a small Catholic high school.
http://dsimmer.com (me)
http://newwaycorktown.org (my missional project)
http://detroitcristorey.org (my employer)

Creating an RSS Feed from our CCK Link List

Dean and I talked on Twitter about extending this tutorial using Views 2 in Drupal 6 and adding an RSS feed to the view. Here's the catch: by default, the feed will link back to the node, not to the directly to the target URL. In some ways, that's not a bad thing, but it's not really what he wanted.

The bad news is, it looks like doing this in a Views 2 Feed is crazy hard, and would require creating a custom style and row plugin as described here.

The FFPC Module is a good example of a custom module that creates its own plugin. There may be some other views plugin modules that would make a good starting point for this. I haven't had a chance to look. In any case, it appears that this isn't something I'll be able to bang out really quickly, as I'd originally hoped.

Micah

Is the fact I am using D6 and

2

Is the fact I am using D6 and Views2 the reason this tutorial doesn't make complete sense??

I have been trying to create links lists with CCK & Views. I can get the info on the page, but just can't get the parts clickable I want and a decent layout.

Tutorial Updated for Drupal 6

HaloFX,

I posted an update for Drupal 6 and Views 2. Hopefully it will help. The choices don't really change, but it definitely looks different in the new Views interface.

Micah

Thanks! But....

3

Thanks for the update. However I am still a little lost, where is the second screen shot coming from?

Link not Links

4

Hey Micah,

Great tutorial and the answer to exactly what I was trying to accomplish. Just one point of reference however and I think it is related to the previous comment. You recommend downloading the Links module when it is really the Link (CCK field) module that you are referring to. I thought I was all set when I read the requirements because I had all but the autonodetitle module installed so I got that in place and went to work. When I didn't see the field type in my list of available fields I knew something was up. Hence I went looking and found the difference. Subtle but enough to make a huge difference.

Here is the link to the Link module: http://drupal.org/project/link

Thanks,
Dave

Fixed. Thanks for the catch!

Fixed module name and link in original article. Thanks for the catch!