0

I have a site which is being developed in Drupal. My developer was developing in the live site directly. No one knew of our existence, so we didn't bother.

I had to change developers midway through the project. When the new developer wanted to develop locally, he had a lot of issues: links not functioning etc. We have found around 25,000 instances of the original site URL.

So what he did was to find and replace all 25,000 URLs with the local site URL in the database itself. Now everything worked fine.

Once the site was perfect locally we moved to live site again by replacing all URLs back to the original URL.

When I checked with my original developer why he had hardcoded, he mentioned that as we wrote a lot of custom code, Drupal automatically created these links. Is this possible?

Will this affect performance or have any long term issues?

  • Can you tell us what this custom code does that native Drupal or existing modules do not provide? I'm not challenging the validity of creating a custom module, but our better understanding of your requirements, and how this code addresses this, will help guide us in our answers. Also, does your site make frequent cross-domain (or subdomain, as kiamlaluno points out) URL requests, or is the vast majority of your site at www.yoursitedomain.com? – Screenack Dec 26 '15 at 20:39
1

If by "a lot of custom code" he meant he created a custom module, then there is nothing that explains why the links were hard-coded. Keep in mind that Drupal has url() and l() to create URLs and links, which removes the need of using hard-coded links when they are links to the same Drupal site. For example, the following code (used by Drupal core) would work correctly even when the code would be moved from a site to another. As a matter of fact, it generates links that work for every site powered by Drupal.

  $link = l(t('Remove orphaned actions'), 'admin/config/system/actions/orphan');
  $count = count($actions_in_db);
  $orphans = implode(', ', $orphaned);
  watchdog('actions', '@count orphaned actions (%orphans) exist in the actions table. !link', array('@count' => $count, '%orphans' => $orphans, '!link' => $link), WATCHDOG_INFO);

To notice that, Drupal normally (as in the above case) generates links that are relative; that is why they work independently from the domain. l() works in many cases, even in the case Drupal is installed in a sub-directory of the server document directory, or when clean URLs are disabled. On my test site, the above code ($link = l(t('Remove orphaned actions'), 'admin/config/system/actions/orphan');) would create the following link.

<a href="/dr71/admin/config/system/actions/orphan">Remove orphaned actions</a>

If I disable the clean URLs, the same code would get me the following link.

<a href="/dr71/?q=admin/config/system/actions/orphan">Remove orphaned actions</a>

The same code would produce the following links, when Drupal is not installed in a sub-directory.

<a href="/admin/config/system/actions/orphan">Remove orphaned actions</a>
<a href="/?q=admin/config/system/actions/orphan">Remove orphaned actions</a>

The only case I can think where absolute URLs are necessary is when the URL is pointing externally to the Drupal site, for example to a sub-domain (to blog.example.com from exampl.com). That is the only absolute URL I have in my Drupal site, since I have added the link from admin/structure/menu/manage/menu-secondary-menu/add.

I could have used a custom module to add the link, and in that case I would have avoided to use an hard-coded link. For example, I could have used the following code.

global $base_url;
$blog_url = preg_replace('@(https?://)(.*)@', '$1blog.$2', $base_url);

In the case of my local test site, the above code would set $blog_url to 'http://blog.tero.local/dr71', while in the case of my Drupal site, it would set the same variable to 'http://blog.avpnet.org'.

A quicker method would be adding to the settings.php file a function that returns the link.

function _get_absolute_url($type) {
  global $base_url;

  switch ($type) {
    case 'blog':
    $url = preg_replace('@(https?://)(.*)@', '$1blog.$2', $base_url);
    break;
  }

  return $url;
}

In this case, for my test site I could use the following code, since I don't have a blog sub-domain.

function _get_absolute_url($type) {
  global $base_url;

  return $base_url;
}

In that way, I don't hard-code 100 links, but I use a single function that changes basing on the settings.php used from the site. In the case I need a link to the blog site, I would use code similar to the following one.

$blog_first  = _get_absolute_url('blog') . 'node/1';
$blog_second = _get_absolute_url('blog') . 'node/2';
$blog_third  = _get_absolute_url('blog') . 'node/3';

The code is assuming that, in the case of link to a Drupal site, that Drupal site is using clean URLs. If you administer that other site/sub-domain, it would not be difficult to change the code to get the correct URL.

0

Answering your question 1: Yes, if you set Drupal to automatically generate or create the links, it will.

Answering your question 2: Drupal it's BIG, Drupal provide a lot of versatility for coders/developer, but generally, even if the site it's also BIG, it will works nicely and fast. Don't worry about that, and contact your developer.

  • Can you put a finer point on "Yes, if you set Drupal to automatically generate or create the links, it will."? "It" will impact performance? Which? The path module? When we say "impact," by what quantifiable value? – Screenack Dec 26 '15 at 20:48

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.