开发者

drupal_add_css not working

开发者 https://www.devze.com 2022-12-18 02:06 出处:网络
I\'m trying to add stylesheets conditionally to my drupal 6 site. I\'ve added the following code to my template.php file to test it, and it\'s not working. The css file does not get added to my site.

I'm trying to add stylesheets conditionally to my drupal 6 site. I've added the following code to my template.php file to test it, and it's not working. The css file does not get added to my site.

function ben_preprocess_page(&$vars) {
drupal_add_css('/sites/a开发者_如何学Pythonll/themes/ben/advice.css','theme','all',FALSE);}

This code works fine when I put it in a custom module I made using hook_init() but I think I need to put it in my template.php file as I want to check the page I'm on.

Any help would be much appreciated.

Ben


I used this code to solve my problem:

function ben_preprocess_page(&$vars) {
        $alias=drupal_get_path_alias($_GET['q']);
        $alias=explode('/',$alias);
        $vars['ben'] = $alias[0]; } 

I can then add $ben to my page.tpl.php inside the body class and make up css class rules based on the different values. e.g. If the path is 'advice' I create a css rule called '.advice'.

Thanks for everyones help, it helped me solve the problem.

Ben


IIRC, the stylesheet links have already been rendered when the *_preprocess_page() functions get called, and the generated markup got placed within $variables['styles']. So it is to late to use drupal_add_css() by then.

You could either assemble the <link ...> markup for your additions yourself and append it to $variables['styles'], or you need to find a better place for your call to drupal_add_css() earlier in the processing chain (probably from within a module).

What place that would be is hard to tell without knowing what you mean exactly by 'check the page I'm on', but if we are talking node pages, hook_nodeapi() would be a candidate.


EDIT after clarification in comments: If the decision on what stylesheets to add is based on the path alone, hook_init (in a custom module) would be a proper place to do it, as the path will not change after that. The only 'tricky' bit in this case would be to get at the clean URLs. If (as I assume) you use clean URLs, you can not use arg(0) to get the first element of the path, as it would return the first element of the Drupal internal path (e.g. 'node' for node pages). So you have to get the clean URL version first:

// Get current path alias, if any (will return original path, if no alias set)
$path = drupal_get_path_alias($_GET['q']);
// Extract first element
$path_elements = explode('/', $path);
// Do we have at least one element?
if (0 < count($path_elements) {
  // Yes, add stylesheet based on that
  switch ($path_elements[0]) {
    case 'advice':
      drupal_add_css('path/to/advice.css');
      break;
    case 'services':
      drupal_add_css('path/to/services.css');
      break;
    // TODO: Add other variations ...
    default:
      // Might add a default alternative here
      break;
  }
}

(NOTE: untested code, beware of typos)


I put the following code in a custom module and it is now appending the css file:

function defprofile_preprocess_page(&$vars) {
drupal_add_css('sites/all/themes/ben/advice.css','theme','all',FALSE);
$vars['styles'] = drupal_get_css(); }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号