Drupal automatically create hardcoded links and do such links impact performance? 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?
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' =&gt; $count, '%orphans' =&gt; $orphans, '!link' =&gt; $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.

&lt;a href="/dr71/admin/config/system/actions/orphan"&gt;Remove orphaned actions&lt;/a&gt;


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

&lt;a href="/dr71/?q=admin/config/system/actions/orphan"&gt;Remove orphaned actions&lt;/a&gt;


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

&lt;a href="/admin/config/system/actions/orphan"&gt;Remove orphaned actions&lt;/a&gt;




&lt;a href="/?q=admin/config/system/actions/orphan"&gt;Remove orphaned actions&lt;/a&gt;


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.