Creating links in Drupal

tagged with

Creating internal hyperlinks in Drupal, from one page to another, can be slightly tricky. Fully qualified links, those that contain the entire address including http, are easy enough to use but you don't want to have to enter a static, fully qualified URL each time you want to link from one page or blog post to another.

For a start, in the event you changed domain all these links would break, and if your site was substantial it would require a lot of work to fix everything. Luckily there is a better way...

Artisteer - CMS Template Generator

Assuming that you are the site administrator/owner, or at the very least, someone with sufficient permissions to use the PHP input format, then there is a Drupal function available to help correctly create links:

The url() function takes a path and generates a URL string from it.

Trying to create links in the standard way:

<a href="content/creating-links-drupal">creating links in Drupal</a>

Will end up with in heartbreak because that relative path is simply added to the current path. For example, if this link was displayed on this site's homepage, it would correctly link to its page. If it showed from somewhere else it would break because it would try to link to content/content/creating-links-drupal (try the static link here).

Note that the relative path has simply been tacked on without regard for whether the page exists.

UPDATE: You can add a leading slash to the relative URL (i.e. /content/creating-links-drupal) to force it to be added to the base path, but this can still lead to problems with changing path aliases, if you're not careful.

This is why we need the url() function to generate the link for us. Our standard link notation now becomes:

<a href="<?php echo url('relative/path/to/content'); ?>">link text</a>

As you can see, we have echo'd the results of the url() function into the href attribute of the anchor tag. Creating links like this ensures that no matter where the link is displayed in a site (it could be from a view, from the front page, from another blog post), it will always be correct (try the url() link here).

Another important point to note pertains to path aliases. If, for example, you are using the Pathauto module to generate aliases for your URLs for SEO purposes, then you should always link to the node ID of the target content because the node ID can never change once the node is created. The alias can change easily. For example, if you decide to alter the title, the alias will automatically change to fit the new title and any links to the old alias will break.

In this case, the above link should be <a href=" <?php echo url('node/238'); ?>">link text</a> (try the node ID url() link here). It's easy to find the node ID of any content by scanning the page source for that page, or clicking edit on that node - assuming you have edit permissions.

Struggling with Drupal?

If you're struggling with Drupal then there are a few steps you can take to try and resolve your problem as quickly as possible:

  1. Search on Google
  2. Search on drupal.org
  3. Ask a question in the Drupal forums

For more complex issues or more involved tasks, you can speak to me. I can solve your problems quickly and efficiently. You can either contract me on an hourly basis for once off projects, or keep me on retainer - in which case, I will be on-hand to solve problems and implement solutions on a regular basis.

I Pledge that my content is 100% unique and original
David Mercer's picture

"Bestselling author of development, eCommerce and marketing books." ~ Wikipedia

I am a serial entrepreneur and startup founder. I also consult to huge corporates and SMEs - providing insight and experience that relates business objectives to technical, analytical, Internet marketing, and SEO solutions. Tell me what your business needs; and I'll make it so.