The color scheme CSS is added as inline CSS instead of as a separate file. It is too much and too persistent to be inline code and it should be added as an external file instead.

Comments

heygabe’s picture

Additionally, this makes customizations to the color themes kind of frustrating. Unless I'm missing something.

jarek foksa’s picture

Status: Active » Postponed (maintainer needs more info)

I fully agree with you that inline styles are ugly and their use should be discouraged. But is there any alternative in this case? AFAIK Drupal Core does not provide API that would allow me to generate external CSS files dynamically or put PHP variables inside already existing CSS files.

xano’s picture

Drupal core already generates CSS files dynamically. Go to Administer > Site configuration > Performance and turn on CSS aggregation. Then check the page's source code and you'll notice only a single CSS file is being used and it's located in /sites/*/files/.

jarek foksa’s picture

By dynamically generated CSS file I meant stylesheet that would be filled with values provided by theme_get_setting() function.

I guess I could reuse those code snippets to get all stylesheets into one variable and then perform some string replacement operations on it, but this would be even less elegant solution than the current one.

xano’s picture

All you have to do is store the CSS you now generate during every page load in a separate file and add that file with drupal_add_css().

jarek foksa’s picture

OK, I see now that this might be possible, but what's the code snippet for storing variable into separate file?

  ob_start();
  include('dynamic.css.php');
  $dynamic_css = ob_get_contents();
  ob_end_clean();
  // add  function that saves $dynamic_css variable into file here
  drupal_add_css($dynamic_css_file_path, array('group' => CSS_THEME, 'preprocess' => FALSE));
nicholas.alipaz’s picture

I got to thinking about how to do this for another project I am working on and found this page via searching. One of the first ideas that came to mind was hook_menu and a custom callback:

/**
 * Implementation of hook_init().
 */
function availability_calendars_init() {
  drupal_add_css(drupal_get_path('module', 'mymodule') . '/mymodule.css');
}

/**
 * Implementation of hook_menu().
 * @return array
 */
function mymodule_menu() {
  $items = array();
  $css = drupal_get_path('module', 'mymodule') . '/mymodule.css';
  $items[$css] = array(
    'page callback' => 'mymodule_css',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Callback for hook_menu().
 */
function mymodule_css() {
  drupal_set_header('Content-Type: text/css');
  print 'body{color:#ffffff!important;}';
}

Creating the css works, loading the menu item does too. drupal_add_css() does not: http://drupal.org/node/329812

So in the end there is no way to make that code work from what I can tell. I even tried doing drupal_add_css() for a real file then using @import to include my page, but that didn't work either.

Alternatively, looking at the core color module may be of use in what we are trying to do.

nicholas.alipaz’s picture

OK, through some research of color module and other things that I came upon, I came up with:

/**
 * Implementation of hook_init().
 */
function mymodule_init() {
  drupal_add_css(file_directory_path() . '/mymodule/mymodule.css');
}

/**
 * Settings page callback.
 * @return array
 */
function mymodule_admin_settings() {
//...
  $form['#submit'][] = 'mymodule_admin_settings_submit';
//...
}

function mymodule_admin_settings_submit($form, $form_state) {
//... get your data from $form_state['values'] and process to create $data...
  _mymodule_save_stylesheet($data);
}

/**
 * Save the stylesheet to disk.
 */
function _mymodule_save_stylesheet($data) {
  $csspath = file_create_path('mymodule');
  file_check_directory($csspath, FILE_CREATE_DIRECTORY);
  $file = $csspath . '/' . 'mymodule.css';

  file_save_data($data, $file, FILE_EXISTS_REPLACE);

  // Set standard file permissions for webserver-generated files.
  @chmod($file, 0664);
}

I hope that helps you guys get your issue sorted. I am not using corolla at the moment, only found this because it related to something I was working on. Good Luck and happy coding.

Jeff Burnz’s picture

Issue summary: View changes
Status: Postponed (maintainer needs more info) » Closed (outdated)