You are here

function ctools_css_cache in Chaos Tool Suite (ctools) 6

Same name and namespace in other branches
  1. 7 includes/css.inc \ctools_css_cache()

Write a chunk of CSS to a temporary cache file and return the file name.

This function optionally filters the CSS (always compressed, if so) and generates a unique filename based upon md5. It returns that filename that can be used with ctools_css_add_css(). Note that as a cache file, technically this file is volatile so it should be checked before it is used to ensure that it exists.

You can use file_exists() to test for the file and file_delete() to remove it if it needs to be cleared.

Parameters

$css: A chunk of well-formed CSS text to cache.

$filter: If TRUE the css will be filtered. If FALSE the text will be cached as-is.

Return value

$filename The filename the CSS will be cached in.

2 calls to ctools_css_cache()
ctools_css_retrieve in includes/css.inc
Retrieve a filename associated with an id of previously cached CSS.
ctools_css_store in includes/css.inc
Store CSS with a given id and return the filename to use.
1 string reference to 'ctools_css_cache'
ctools_update_6002 in ./ctools.install
Add the new css cache table.

File

includes/css.inc, line 145

Code

function ctools_css_cache($css, $filter = TRUE) {
  if ($filter) {
    $css = ctools_css_filter($css);
  }

  // Create the css/ within the files folder.
  $path = file_create_path('ctools/css');
  if (!file_check_directory($path)) {
    $path = file_directory_path() . '/ctools';
    file_check_directory($path, FILE_CREATE_DIRECTORY);
    $path .= '/css';
    file_check_directory($path, FILE_CREATE_DIRECTORY);
  }
  if (!file_check_directory($path)) {
    drupal_set_message(t('Unable to create CTools CSS cache directory. Check the permissions on your files directory.'), 'error');
    return;
  }

  // @todo Is this slow? Does it matter if it is?
  $filename = $path . '/' . md5($css) . '.css';

  // This will do renames if the file already exists, ensuring we don't
  // accidentally overwrite other files who share the same md5. Yes this
  // is a very miniscule chance but it's safe.
  $filename = file_save_data($css, $filename);
  return $filename;
}