- Home
- Blogs
- George Boobyer's blog
- Doing it the Drupal way...
Doing it the Drupal way...
It is quite common practice to add head elements such as:
- metatags and links such as webfont links and
- xmlns namespaces
as text to the html.tpl.php or in template.php in hook_preprocess_html etc and other string based ways of doing things.
The problem with this is that it is hard coded and not the Drupal way. I have cobbled together some examples of how we do this in code (most of which have been gleaned from issue queues, posts and other great Drupal.org sources - credit to many anon contributors! The point here is simply to pull together some examples.
Drupal has $rdf_namespaces and $head variables to handle these for 'html.tpl.php' and ideally you would use these.
So a better way is to add them programmatically:
So instead of: i.e. in html.tpl.php
<html xmlns:fb="http://www.facebook.com/2008/fbml" xmlns:og="http://opengraphprotocol.org/schema/" xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php print $language->language; ?>" version="XHTML+RDFa 1.0" dir="<?php print $language->dir; ?>"<?php print $rdf_namespaces; ?>>
do this in your module:
<?php /** * Allow modules to define namespaces for RDF mappings. * * @return * An associative array of namespaces where the key is the namespace prefix * and the value is the namespace URI. * This enables you to add your own xmlns entries beyond the default set. */ function mymodule_rdf_namespaces() { return array( 'fb' => 'http://www.facebook.com/2008/fbml', ); } ?>
and add metatags (and other head elements) in hook_html_head_alter (in your module or theme template.php code)
so instead of:
$variables['meta'] .= '<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">' ;
do it this way:
<?php function mytheme_html_head_alter(&$head_elements) { //some like to remove the credit to Drupal unset($head_elements['system_meta_generator']); //simplify the charset $head_elements['system_meta_content_type']['#attributes'] = array( 'charset' => 'utf-8', ); // Force the latest IE rendering engine and Google Chrome Frame. $head_elements['chrome_frame'] = array( '#type' => 'html_tag', '#tag' => 'meta', '#attributes' => array('http-equiv' => 'X-UA-Compatible', 'content' => 'IE=edge,chrome=1'), ); // so instead of // <link rel="stylesheet" href="http://f.fontdeck.com/s/css/myreallylongreferencetofontdeck.css" type="text/css" /> // // add fontdeck code $head_elements['fontdeck'] = array( '#type' => 'html_tag', '#tag' => 'link', '#attributes' => array( 'href' => 'http://f.fontdeck.com/s/css/myreallylongreferencetofontdeck.css', 'rel' => 'stylesheet', 'type' => 'text/css', ) ); // adding things this way enables yu to interrogate the list of head elements // // and target any you want to change or alter // e.g. remove canonical and shortlink tags specifically foreach ($head_elements as $key => $element) { if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'canonical') { unset($head_elements[$key]); } if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'shortlink') { unset($head_elements[$key]); } } // or remove them using an array $remove = array( 'system_meta_generator', 'metatag_canonical', 'metatag_shortlink' ); foreach ($remove as $key) { if (isset($head_elements[$key])) { unset($head_elements[$key]); } } } ?>
So what is the advantage of this?
Well it is easier to interact with and you can check to see if another module is adding similar tags, etc
and you can optionalise it should you need to.
Contact Details
Blue-Bag Ltd
- info [at] blue-bag.com
- Telephone: 0843 2894522
- Blue-Bag HQ:
The Garage, Manor Farm
Chilcompton, Radstock
Somerset, BA3 4HP, United Kingdom - Telephone: (+44) 01761 411542
- Blue-Bag Brighton:
Unit 35 Level 6 North, New England House
New England Street, Brighton
BN1 4GH United Kingdom - Telephone: (+44) 07944 938204
- VAT GB 748125034
- UK Company Reg: 3932829