The drop is always movingYou know that saying about standing on the shoulders of giants? Drupal is standing on a huge pile of midgetsAll content management systems suck, Drupal just happens to suck less.Popular open source software is more secure than unpopular open source software, because insecure software becomes unpopular fast. [That doesn't happen for proprietary software.]Drupal makes sandwiches happen.There is a module for that

Serving static content from a different domain in Drupal 7

Submitted by nk on Wed, 2010-08-11 19:14

We wanted to serve static content from a CDN -- but it's the same if you only want to move your static content to a different server. This is fairly well researched and done in Drupal 6, here is the Drupal 7 version. It seems that some theme stuff (like the favicon and the logo and probably unaggregated CSS/JS) is passed as Drupal paths into file_create_url while aggregated CSS and JS is served from public://. So we need to implement file_url_alter and override the getExternalUrl of the DrupalPublicWrapper class (thanks goes to Damien Tournoud for pointing out the latter):

function mymodule_file_url_alter(&$uri) {
  static $drupal_static_fast;
  // A drupal_static_reset() would clear this and then we need to rerun variable_get.
  if (!isset($drupal_static_fast['cdn_domain'])) {
    $drupal_static_fast['cdn_domain'] = &drupal_static(__FUNCTION__);
    $drupal_static_fast['cdn_domain'] = variable_get('cdn_domain');
  }
  $cdn_domain = &$drupal_static_fast['cdn_domain'];
  if ($cdn_domain && strpos($uri, ':') === FALSE) {
    $uri = $cdn_domain . $uri;
  }
}

function mymodule_stream_wrappers_alter(&$wrappers) {
  $wrappers['public']['class'] = 'DrupalCDNPublicStreamWrapper';
}

class DrupalCDNPublicStreamWrapper extends DrupalPublicStreamWrapper {
  function getExternalUrl() {
    $path = str_replace('\\', '/', $this->getTarget());
    $prefix = variable_get('cdn_domain', $GLOBALS['base_url'] . '/');
    return $prefix . self::getDirectoryPath() . '/' . drupal_encode_path($path);
  }
}

Commenting on this Story is closed.

Submitted by moshe weitzman (not verified) on Wed, 2010-08-11 20:49.

those are some very handy lines. thanks chx.

Submitted by Anonymous on Thu, 2011-08-04 15:17.

I am once again feeling happy and proud to say that this is my favorite web site.The postings are very unique and also out standing performance with the new creativity and excellency with the new different ideas and concepts.Really I am waiting for some more new posts from you.Keep up your excellency and efficiency in this same levels. utah fire systems

Submitted by Wim Leers (not verified) on Wed, 2010-08-11 22:20.

hook_file_url_alter() is called for all file URLs that are generated. The sample implementation of hook_file_url_alter() (in the docs at http://api.drupal.org/api/function/hook_file_url_alter/7) shows how you can alter *all* file URLs through a single function.
This sample implementation also has unit tests, so it's guaranteed to work.

Note: I contributed hook_file_url_alter() into Drupal 7, to simplify CDN integration in Drupal.

Submitted by nk on Thu, 2010-08-12 01:23.

that example is basically string parsing out what's being put together in getExternalUrl(). That's not particularly performant. Overriding the schema wrapper is so much faster. Note that the public scheme for the aggregated CSS/JS is hardwired so this solution works alongside whatever else schema wrapper you use.

Submitted by apasajja (not verified) on Wed, 2010-08-11 23:03.

Hi, where is codes for drupal 6? Thanks

Submitted by sun@drupal.org on Thu, 2010-08-12 03:10.

  static $drupal_static_fast;
  // A drupal_static_reset() would clear this and then we need to rerun variable_get.
  if (!isset($drupal_static_fast['cdn_domain'])) {
    $drupal_static_fast['cdn_domain'] = &drupal_static(__FUNCTION__);
    $drupal_static_fast['cdn_domain'] = variable_get('cdn_domain');
  }
  $cdn_domain = &$drupal_static_fast['cdn_domain'];

Any reason for not using the regular $drupal_static_fast pattern as documented?

  $cdn_domain = &$drupal_static_fast['cdn_domain'];

The reference might be overhead here.

funcion mymodule_stream_wrappers_alter(&$wrappers) {
  $wrappers['public']['class'] = 'DrupalCDNPublicStreamWrapper';
}

To be on the safe side, this should be wrapped in the following condition:
  if ($wrappers['public']['class'] === 'DrupalPublicStreamWrapper') {
    $wrappers['public']['class'] = 'DrupalCDNPublicStreamWrapper';
  }

...which is actually one of the reasons why my gut feeling still is that OOP, or at least the OOP usage we've invented so far, is not right, no, actually kills Drupal's awesome modularity.

In other words, I agree with Wim that replacing the stream wrapper class is wrong from a modular API perspective.

Submitted by nk on Thu, 2010-08-12 06:10.

When you reset the drupal_static variable, it will become NULL so it's reloaded by variable_get -- the presumption is that a drupal_static_reset variables might change too during testing. Having a single additional reference for simplicity does not matter much in speed, it can be omitted yes.

Whether it's OOP or not, if it's a singular callback then there is only one that can do it. You mean that if another class takes over another method...? Yes. This is a problem. Agreed.

Submitted by sun@drupal.org on Fri, 2010-08-13 19:40.

Right. So your module exchanges the class to do one thing, so no other module is able to do another thing.

Really, I still don't get why and how so many people are promoting and adopting OOP code and patterns for Drupal. I'm constantly hitting the same limitations all over again, and people are just telling me: "well, that's how OOP is supposed to work." WTF? The only remotely acceptable and compatible solution attempts I've seen so far are those by fago ("facets" (?) or similar).

Submitted by nk on Mon, 2010-08-16 23:57.

Regardless of syntax, if you have one callback doing stuff and you want to exchange it -- tough luck? Node hooks, field hooks...?

Edit: I am wrong here somewhat because the problem here is that the class ties together all behavior.

Submitted by Anonymous on Wed, 2010-11-10 03:37.

wedding gowns

lace wedding gowns

wedding gown

wedding dresses