Creating links in Drupal
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...
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:
Trying to create links in the standard way:
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:
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:
- Search on Google
- Search on drupal.org
- 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.
"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.
"World class content, business, SEO & Internet marketing services!"
David Mercer
Get the weekly expertise, strategies & tips you need to succeed online!
Current projects
OO PHP Web service ACO solution to TSP
Building an OO PHP web service application using ACO (Ant colony optimization) as a stochastic and heuristic way to solve the TSP (Traveling Salesman Problem) for the UK vehicle and fleet tracking firm 3D Tracking.
"Doctoral level maths, OOP, PHP and ants. Having nightmares about ants... ;)"
PHP and Views customizations for Drupal startup
Doing some heavy Views and PHP customizations for an SA startup called ZAPlayGround (not yet launched). Just worked out that Views won't pick up taxonomy terms from URL (path) aliases in organic group contextual block.
Provided a PHP default argument script to manually generate the path alias from the NID.
"It's working! Now to clone the view and away we go."
Business, marketing, SEO, social web analysis
Large corporate - NDA'd. Looking at business requirements, and how to improve the workflow with regards to creating social and SEO marketing content in the most efficient way. Complex requirements. Lots of stakeholders.
"Always interesting working with large organizations. Getting things done requires approval in triplicate ;)"



















