Quantcast
Viewing all articles
Browse latest Browse all 292190

Storing aggregated CSS and JS cache files outside of public://

I'm trying to solve for a scenario and have a mostly working solution and was hoping you guys could help.

Problem:
We wish to hold aggregated css cache locally while retaining everything else externally through public:// to our external host.

Environment:
~ Drupal 7.22, PHP 5.4.11
~ File system: Public: sites/default/files
~ File system symlink: sites/default/files is a symlink to an external file server location

Solution (so far):
~ Created a module which adds a stream wrapper "foobar://" (works)
~ Module hook_form_alters into the Admin > File System (works)
~ The hook into Admin > File System creates the directory (works)

Where it gets hairy:
In includes/common.inc, drupal_build_css_cache and drupal_clear_css_cache both have the hardcoded css storage path of 'public://css'. I need to change this to 'foobar://css'. Neither of these have a hook.

Working solution fordrupal_build_css_cache:
Backtracking a little, I found system_element_info in system/system.module that declared the aggregate_callback. hook_element_info_alter gave me a way in.

function mymodule_element_info_alter(&$type) {
  if (isset($type['styles']['#aggregate_callback'])) {
    $type['styles']['#aggregate_callback'] = 'mymodule_drupal_aggregate_css';
  }
}

I then pulled drupal_aggregate_css into my module and changed drupal_build_css_cache to mymodule_drupal_aggregate_css

...
      case 'file':
        if ($group['preprocess'] && $preprocess_css) {
          $css_groups[$key]['data'] = mymodule_drupal_build_css_cache($group['items']);
        }
        break;
...

Lastly, mymodule_drupal_build_css_cache is a clone of drupal_build_css_cache with the exception of the path to the filesystem.

...
    $filename = 'css_' . drupal_hash_base64($data) . '.css';
    // Create the css/ within the files folder.
-  $csspath = 'public://css';
+  $csspath = 'foobar://css';
    $uri = $csspath . '/' . $filename;
    // Create the CSS file.
    file_prepare_directory($csspath, FILE_CREATE_DIRECTORY);
...

That all works. Yay.

Where I'm stuck:
drupal_clear_css_cache is called from more places than the above and I couldn't find an obvious way to hook it.

Any thoughts would be greatly appreciated!


Viewing all articles
Browse latest Browse all 292190

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>