0

We are trying to make a Drupal mini bootstrap for our language selection module. We implemented hook_boot(), in this function, we included path.inc and common.inc from core.

The language selector always redirects to the frontpage of the according language. Now the problem is that, when loading the new page, the class of <body> is not-front. This means that front-specific styles, and template are not used; it just uses the default ones.

We are guessing that we missed something in the Drupal bootstrap process.

Do you have any idea?

1

If memory serves me correct the "front" class will only be set on the body, if the URL you are on matches what is set in the "Site Information" > Front Page field.

My guess is that you'll need to override the string or array of body classes in the template.php file of your theme. To do this you'd want to use the template_preprocess_html function provided by Drupal core.

Here is a quick and dirty example of how to add the class to the body of your theme:

function THEMENAME_preprocess_html(&$variables) {

  if (theme_get_setting('my_variable') == 0) {
    $variables['classes_array'][] = 'front';
  }

}

From there you'd need to figure out what classifies the home page as the home page with some conditionals or custom functions.

  • thanks for the fast answer! we tried it out, now it uses the right front-template but the body tag still has the "not-front" class. where does drupal decide about what has to be in there? – katomat Jul 3 '12 at 16:40
1

The function that set those CSS classes is template_preprocess_html(), which uses the following code.

  // Add a class that tells us whether we're on the front page or not.
  $variables['classes_array'][] = $variables['is_front'] ? 'front' : 'not-front';
  // Add a class that tells us whether the page is viewed by an authenticated user or not.
  $variables['classes_array'][] = $variables['logged_in'] ? 'logged-in' : 'not-logged-in';

$variables['is_front'] is set by _template_preprocess_default_variables(), which is called by template_preprocess(), and contains the following code.

  // drupal_is_front_page() might throw an exception.
  try {
    $variables['is_front'] = drupal_is_front_page();
  }
  catch (Exception $e) {
    // If the database is not yet available, set default values for these
    // variables.
    $variables['is_front'] = FALSE;
    $variables['db_is_active'] = FALSE;
  }

$variables['classes_array'] is then imploded in template_process(), which uses the following code.

  // Flatten out classes.
  $variables['classes'] = implode(' ', $variables['classes_array']);

I tried the following text code, in my test site.

function code_testing_preprocess_html(&$variables) {
  $variables['classes_array'][] = 'front';
}

For a page that is not the front page, I got the following output.

<body class="html not-front logged-in one-sidebar sidebar-first page-tracker front toolbar" >

Your code should remove "not-front" as CSS class, before adding "front"; this could done in hook_process(), by altering $variables['classes'], or in hook_preprocess_html(), by altering $variables['classes_array'].

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.