Drupal: writing links that work on website AND computer

I have an ongoing annoyance with Drupal. Whenever I have to use a full link in a template, I have no choice but to write the link to match our domain, knowing that it will not work on my computer. I put up with it, but I have to keep two versions of the HTML template (one for the website and one for my computer). The comment template is awkward as well, because without a full link, the reply links would go wrong. The code that can be used in the page template to print the domain mysteriously fails in the other templates.

There are a number of ways to achieve this and Drupal was built with this in mind. You should never have to use a full direct path, you should be able to use relative paths or one of Drupal’s functions to render the path.

The easiest way to get root is “/”. That should work in 99% of situations. If you need more granular control, for instance to find the path to the theme you can use:


 'sites/all/themes/mytheme/css/some-css-file.css' 

or you can leverage PHP and use:


<?php print base_path() . path_to_theme(); ?>/css/some-css-file.css

I just looked through a bunch of my D7 Drupal sites and see that I rarely use base_path() or path_to_theme() anymore. I usually use “/” for a relative path. My D6 sites used a lot of the PHP functions though. I try to limit PHP calls in templates for less server load.

Another thing to keep in mind is that if you link to pages within text content inline, link to the node. ie: <a href=“/node/34”>this is a link</a> that way unless node/34 is deleted, you will never break the link. I use the Global Redirect module so that when node/34 is called, the url is rewritten behind the scenes to a seo friendly url.

I hope that sheds some light on things for you.

Andrew

Your example for CSS would not work. Well, I mean that it would work for a few pages, but not for the majority.

You say that I should never need a full path, but I have numerous instances that disprove that because using a relative path that works in one scenario will cause problems at other times. The comment templates are just one example. At the moment, I have to use full links in various templates that get reused at different levels but, for some unknown reason, will not accept the code that works in the page template to print the domain name. I do not understand why the site refuses to use that code in other templates. If it did, I would just use that and I would never get caught by uploading the wrong HTML template, with predictable results.

Yes, unless you are on a CPanel test site where the DNS hasn’t resolved you shouldn’t ever have to use full paths (like http://www.example.com/sites/all/themes/mytheme/somefile.etc).

I’ve got some 40+ Drupal sites running around and none of them use full paths, they are all either relative using the API calls like <?php print base_pathCOLOR=#007700 . path_to_themeCOLOR=#007700; [/COLOR]?>/css/some-css-file.css.

Post a piece of template code you’re having trouble with and I’ll have a look.

Andrew
[/COLOR]

Ok, I do not know what CPpanel or DNS are. Please let’s avoid confusing acronyms. Here are some snippets, as requested:

<head>
<link rel=“stylesheet” media=“all” href=“http://localhost/drupal/sites/all/themes/themetwo/style/themetwo.css” />
<link rel=“stylesheet” media=“only screen and (min-device-width: 320px)” href=“http://localhost/drupal/sites/all/themes/themetwo/style/themetwo-media.css” />
</head>

To summarise my question, I would like something which can produce http://localhost/drupal/ and http://domain.org depending upon where the HTML template happens to be. It would prevent the mistakes that can occur occasionally if I need to update the HTML template but forget to change that part of it before putting on the website. And just to reiterate: omitting the domain/localhost part does not work for more than a few basic pages.

You’re hilarious… I seriously almost spit coffee at my screen when I read that. The internet is nothing, if not a muddle of acronyms. CPanel and DNS are the easy ones, I promise you that.

Now, I assume these snips are from page.tpl.php in Drupal 6 or html.tpl.php in Drupal 7.

Here’s problem number 1. You have Drupal installed in a sub-folder called drupal rather than in the root which means using / as a relative path gets you one directory below the drupal site.

Important: When you are working on a drupal site or any CMS for that matter, you need to install it in the root. It doesn’t matter how it gets into the drupal folder because I realize a great deal of automatic software installers will stuff it into a sub folder; if you’re the developer, it’s up to you to know the directory structure of your server and to make sure Drupal is installed in the root directory.

So with this in mind, you’ll have to use the PHP snipets I mentioned in my first post to make this work.



<head>
<link rel="stylesheet" media="all" href="<?php print base_path() . path_to_theme(); ?>/style/themetwo.css" />
<link rel="stylesheet" media="only screen and (min-device-width: 320px)" href="<?php print base_path() . path_to_theme(); ?>/style/themetwo-media.css" />
</head>


Pardon me? Do you tell your clients that they are hilarious when they do not know everything that you know about web design? Or do you just save that for forum users? You are usually very friendly and helpful, but that comment comes across as insulting and patronising. My profile mentions that I’m only doing this as a hobby project, so I can’t be expected to know all the jargon. Apart from that, thank you for the code solution.

I think we should get something straight from the get-go… You are not my client. You are someone who asked for help and up until now has not once thanked me for my time during this discussion. I help people with things I know about freely because in the long run people help me and have helped me in all the years I’ve been online.

I’m glad that solution worked for you. I thought it was quite entertaining that you chastised me for using common acronyms rather than saying a polite thank you but I’m not quite sure what that means. I really did laugh when I read that… It struck me as incredulous to be spoken down to for answering a plea for help.

I don’t really wish to keeping this going, but I ought to respond to you. I always thank people once they have provided assistance, which I did here, despite feeling rather offended by your uncharacteristic comment. No, I am not your client: but this does not make it OK to be rude and expect me not to say anything about it. And I did not chastise you (or speak down to you) about acronyms, but asked you to “please” not use confusing acronyms. It is a perfectly reasonable request, I think, and I am sorry if you misinterpreted it, but there was no need for the wording of your response. Anyway, I shall wish you a good night.

Well therein lies our difference in the way you and I operate. If I post a request for help and someone answers it, even for more clarification, I thank them for responding. I don’t wait for them to resolve the issue; they owe me nothing yet they’ve taken the time to see if they can help me. You don’t have to respond in kind but I’ll expect it anyway and it will determine how I respond.

Beyond that, I’m glad you’re issue is resolved because it will truly make the work you’re doing a lot more productive.

Have a good night.

Andrew