Format Drupal 7's Head Output

I am a technology professional committed to the development of sustainable solutions that deliver results.

Jul
23
Format Drupal 7's Head Output

The formatting of the head tags (meta, keywords, author, scripts, icons, etc.) Drupal 7 outputs by default leave lots to be desired vis-a-vis output formatting.

Cleaning it up is actually much simpler thank you think. You simply need to create a formatting function that uses the PHP's preg_replace() function to handle your output.


Step 1.

Create the following function in your theme's template.php or html.tpl.php file:

function yourtheme_format_head_output($input){
$extraformatting = <<< EOF
   
   
EOF;

  $output = preg_replace('/\n+/',$extraformatting,$input);
  return $output;
}

Important: What you can't see between the EOF open and close is that each line has a tab in it. Adjust this for the number of tabs and/or line breaks between lines that you want to use. Just experiment until you get what you want, then:

Step 2.

Wrap the head, styles, and scripts objects with your cleaning function. Look for:

<?php print $head; ?>
<?php print $styles; ?>
<?php print $scripts; ?>

...and change them to:

<?php print yourtheme_format_head_output($head); ?>
<?php print yourtheme_format_head_output($styles); ?>
<?php print yourtheme_format_head_output($scripts); ?>

And that's it. You now have indented/formatted head elements.

Note: You may have to flush your caches to see the changes.