1

I am trying to apply a CSS style (games.css) to page--type--games.tpl.php. I am using Drupal 7, and I have created a new content type called "games."

So far, my code works for pulling up the page--type--games.tpl.php, but it has been unsuccessful in applying the CSS stylesheet. This is my current version of the code.

/**
 * Add javascript files for front-page jquery slideshow.
 */
if (drupal_is_front_page()) {
  drupal_add_js(drupal_get_path('theme', 'bluemasters') . '/js/bluemasters.js');
}

function bluemasters_preprocess_page(&$variables, $hook) {
  //some other stuff
  if (isset($variables['node'])) { 
    $variables['theme_hook_suggestions'][] = 'page__type__'. $variables['node']->type;
    $variables['theme_hook_suggestions'][] = "page__node__" . $variables['node']->nid;
  }

  if (drupal_get_path_alias("node/{$variables['#node']->nid}") == 'games') {
    drupal_add_css(drupal_get_path('theme', 'bluemasters') . "/games.css");
  }
}
0

drupal_get_path_alias("node/{$variables['#node']->nid}") will be equal to "games" only in the case "games" is the path alias for the node being shown.

If you just want to verify the content type of the node being shown is "games," then you need to use code similar to the following one:

if ($variables['node']->type == 'games') {
  drupal_add_css(drupal_get_path('theme', 'bluemasters') . "/games.css");
}

To notice that a page preprocess function is called also for pages that are not associated to nodes; the function should first verify if it gets a node object, before to access it.
The complete code of the preprocess function should be like the following:

function bluemasters_preprocess_page(&$variables, $hook) {
  if (!empty($variables['node'])) { 
    $variables['theme_hook_suggestions'][] = 'page__type__'. $variables['node']->type;
    $variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->nid;

    if ($variables['node']->type == 'games') {
      drupal_add_css(drupal_get_path('theme', 'bluemasters') . "/games.css");
    }
  }
}
  • I revised my code but now when I refresh the page I get this error box popping up: Notice: Undefined index: #node in bluemasters_preprocess_page() (line 18 of /homepages/14/d202984695/htdocs/website/spidermaps-com/smap/sites/all/themes/bluemasters/template.php). Notice: Trying to get property of non-object in bluemasters_preprocess_page() (line 18 of /homepages/14/d202984695/htdocs/website/spidermaps-com/smap/sites/all/themes/bluemasters/template.php). – Laine Jun 3 '11 at 21:19
  • @Laine I updated my answer. – kiamlaluno Jun 3 '11 at 21:26
  • Hi Kiamlaluno, thanks for your help. I think I may be doing something wrong within Drupal (with the games node) because the revised code is still brining up errors. For now, I have just added to the style.css in order to make the design changes I want for the one page. I will update should I find where I went wrong. Thanks again! – Laine Jun 3 '11 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.