Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a preprocess_html function that gets an array of google fonts and adds them as an html class. I have it partially working but if say I have three fonts, only the last one gets into <html class=""> element. Here is what I have so far:

if ($families) {
   foreach ($families as $fontString) {
   $fontclass = preg_replace('/[^\d\w]+/', '-', strtolower($fontString));

   $element = array(
     '#tag' => 'html',
     '#attributes' => array(
     'class' => $fontclass,
    ),
   );
   drupal_add_html_head($element, 'google_fonts');
  }
}

In the example above, I have these fonts in my site:

  • Montserrat
  • Actor
  • Pathway Gothic One

... but only the last one renders in my html class as:

<html class="pathway-gothic-one-regular">

If I do a dsm within my function:

dsm(drupal_get_html_head());

I see all three fonts print out in separate boxes as

<html class="actor-regular" />
<html class="montserrat-regular" />
<html class="pathway-gothic-one-regular" />

so I think I am not getting something right here.

Note, if I change my code to a body class array, it works as expected. e.g.

   if ($families) {
          foreach ($families as $fontString) {
            $vars['classes_array'][] = preg_replace('/[^\d\w]+/', '-', strtolower($fontString));
          }
        }

yields:

<body class="actor-regular montserrat-regular pathway-gothic-one-regular">

From a build standpoint, I'd like to keep these classes one level higher than body so that's why I'd like them to be in <html class="">.

share|improve this question
    
Try moving the $element = array(...) to above the foreach with no class, then just target the class inside the foreach: ie. $element['#attributes']['class'][] = $fontclass. And move the drupal_add_html_head out of the foreach to the bottom –  J. Reynolds Sep 13 '14 at 13:37
    
Brilliant, that works, thanks. You should add that as an answer and I will mark it as such. –  Danny Englander Sep 13 '14 at 13:46

1 Answer 1

up vote 1 down vote accepted
if ($families) {
    $element = array(
        '#tag' => 'html',
    );
    foreach ($families as $fontString) {
        $fontclass = preg_replace('/[^\d\w]+/', '-', strtolower($fontString));
        $element['#attributes'][class'][] = $fontclass;
    }
    drupal_add_html_head($element, 'google_fonts');
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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