Syndicate

Syndicate content

How to add a CCK field to a link in a block in Drupal

That title is a bit "the house that Jack built" so let me explain what this is about. Let's say you have a Drupal website and you're using CCK to hold some piece of data or content. Let's also say you want to have a block which takes that CCK field's content and adds it to a link (which could be internal or external). This is one way to do that.

First, ensure your CCK field has been created. You'll also need Views, so install that.

Now create a new View with a block display.

Click on the plus symbol to add a "Field", scroll down to find your CCK field and click Add.

Now select adjust output of field and you'll see some new options. Here you can create (code) your link, and use the field's tag (like [field_mycckfield]) wherever you would like it to appear.

For example:
<a href="http://www.dandurick.com/[field_CCK]">MY LINK TEXT</a>

Click "Update".

Of course you only want the block to appear on the content type which actually has the CCK field, so we'll need to create an Argument. Use the following paramaters for it:
Node: Nid
Provide default argument
Node ID from URL
Validater - Node
Types (whatever type you want it to appear on)
Argument Type - Node ID

Save the view, and navigate to your blocks admin page and set it to appear in the region you want.

Now, you'll see a block on all nodes of that content type with a link in the region with the contents of your CCK field within the URL of the link.

Where you coded the link (in the Output section of the View) you can also add text around the link to make it a part of a sentence, something like "Read more about..."

This was a pretty specific need that I had, but maybe you can benefit from this technique as well.

Comments