You are here

Adding a custom link to a Drupal 7 Views Template

Anyone familiar with Drupal knows Views. It's great for pulling information from the database and displaying it in just about any format you want. I recently set up a job listing site, which was pretty trivial task in Drupal (I've seen guides on setting up job listing sites in a few Drupal books). The problem I had was when I needed to add a separate "read more" link to the job listing after the description text.

Image of the job listing site

Overriding the default output on the table was simple… Views shows the list of suggested template override filenames under "Advanced → Other → Theme: Information". In my case, the template to create "theme_directory/templates/views-view-field--list-all-jobs--page--body.tpl.php". I started by copying the content of "/sites/all/modules/views/theme/views-view-field.tpl.php" and modifying to taste. Creating the "read more" link to that row's node was done using the following code.

print $output; echo '<a href="', url('node/'. $row-&gt;nid, array('absolute' =&gt; TRUE)), '" class="readMore">Read more…</a>';

The documentation for the function url(...) can be useful in understanding what's happening here. Basically, we're using the node ID to make the URL "node/X", and then asking the "url" function what the absolute URL for that path should be. This returns the full path for that link as defined in the alias for the node.

Topic: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.