0

I want to check all templates before rendering and replace links
node/1 node/3 ect to their aliases

For example, i have main landing page template
page--front.tpl.php
and there are some links in this template:
<a href="node/1">Contact</a>
I want drupal to dynamically change such links to their aliases:
<a href="contact">Contact</a>
So, if i change alias for specific page, all urls will remain correct

Is it possible?

2

Not sure if this is what you're after, but in your tpl.php file you can change

<a href="node/1">Contact</a>

to

<a href="<?php print url('node/1');?>">Contact</a>
-1

It sounds as though you have links hard coded into the template, which is probably a mistake. Can you replace the links with a menu or a block so they are easier to maintain?

However, to answer the question, you can use drupal_get_path_alias('node/1') to obtain the alias. Be sure to prefix with a / as '/contact' may be different from 'contact' in different locations on the site.

You could also use the l() function to make the links: l('Contact', 'node/1').

I would really recommend trying to turn these links into a menu if you can and then putting that into the right template region using 'Blocks'. Your future self will thank you.

  • even core themes card-code some links in tpl files, to say it is probably a mistake is misleading without knowing the context or reasoning behind it. blocks and menus are not king and just as difficult to maintain if done improperly, and often more difficult to theme – Geoff Jun 25 '15 at 3:05
  • Hard coded links to nodes that may or may not exist? Really? – Andy Jun 26 '15 at 3:56
  • Lost the rest of my comment: Hard coded links to nodes that may or may not exist in core templates? Really? Maybe 'probably' was too strong, had I said 'Alway' or 'Indisputably' then I'd agree that my statement was misleading, but in general isn't it better to provide a link that can be maintained through the UI, particularly if there's more than one? It depends who has to maintain the site, and how hard the layout will be using the inbuilt structures, but in general? – Andy Jun 26 '15 at 4:04
  • I do think it's misleading (and somewhat irrelevant now that OP chose a different answer) - and anything hard-coded, modules included can contain dead links or similar problems if done improperly making maintenance difficult, it's all in the delivery. I didn't suggest it was a good idea to do, or that your answer was wrong, only that how you worded your answer seemed prejudicial to me – Geoff Jun 26 '15 at 4:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.