2

I have one site (same codebase, same database) that uses two different domains to display two different themes, depending on the content. (This is achieved via the Domain Access module.)

I need to be able to link from one domain to a view on another domain without manually inserting the absolute URL, but I'm not sure how (or even if it's possible).*

I can use l() to link to nodes without any problems. For example, if I'm on first-domain.com and I want to link to second-domain.com/news-story, I would do the following:

<?php print l('News Story', 'node/2213'); ?>

I don't know exactly how it works, but it realises that the news story isn't available on the first domain and automatically creates the absolute link to the second domain.

Creating a link to a view doesn't seem to be so easy. I've made sure the view's Access restrictions is set to Domains (and that only the one domain is selected) but the l() function doesn't seem to look this up.

For example, if I'm on first-domain.com and want to link to a list of all the news stories (which is created by a view) on second-domain.com, doing this:

<?php print l('Latest News Stories', 'news-stories'); ?>

Results in a link like this:

first-domain.com/news-stories

When I really want: second-domain.com/news-stories

Is there any way to solve this? Even if I could look up both domains that have been set via the Domain Access module would work, but I don't know how to do that either. (I've also thought about using Path Redirect, which is better than nothing, but not quite what I'm looking for.)

* Obviously it's possible to put in an absolute URL, but this is far from ideal as the site is currently being developed locally and will eventually be moved to a staging site, and then moved again to production once ready. Remembering to change the absolute links every time would be annoying at best.

1 Answer 1

1

Since you obviously are capable of PHP, the best way I can think of is to write a custom wrapper for l(), something like:

function ml($text, $path, int $domain = NULL, array $options = array()) {
  switch ($domain) {
    case 1:
      return l($text, 'http://first-domain.com/'.$path, $options);
      break;
    case 2:
      return l($text, 'http://second-domain.com/'.$path, $options);
      break;
    default:
      return l($text, $path, $options);
      break;
  }
}

That way, the absolute domains only have to be changed in this one central place with each staging step (domains may even be stored in settings.php).

2
  • Thank you so much--you've just saved me a TON of time! (Just a minor change: PHP complained about $domain = 0, so I changed it to $domain = NULL.)
    – jelina
    Apr 27, 2012 at 3:43
  • You're welcome. Also, I changed the $domain parameter to default to NULL.
    – Paul
    Apr 27, 2012 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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