Meta descriptions in Drupal 7

Drupal 7 is a great CMS but it really lacks a good meta description module. The best I found is Meta tags quick. This module works fine if you use nodes. But if, like me, you love to use views to design your pages, this module is not able to generate a meta description for these kind of pages. So I decided to make my own function to manage meta description for all pages.

The code below is specific to this website. You will have to adapt it to fit your project.

template.php

This function is made to generate a unique and relevant meta description for each page.
Put this code in template.php (in your theme folder). Adapt default strings to your website content.

<?php
/**
* Create meta description and keyword elements in drupal 7
*
* Trying to generate unique and relevant meta tags
*/

function ic_preprocess_html(&$variables){

 
// Set fallback strings
 
$default_description = t('Web design agency in Cambodia, graphic design, web applications');
 
$default_keywords = t('iconsulting, web design, web design cambodia');
 
$default_site_name = t('IConsulting');

 
// Detect page type
 
if (isset($variables['page']['content']['system_main']['nodes'])) {
 
$page_type = 'single_node';
  } elseif (isset(
$variables['page']['#contextual_links']['views_ui'][1][0])) {
 
// TODO: this view page detection suppose to enable contextual links.
  // find a better way
 
$page_type = 'view';
  }

  switch (
$page_type) {
    case
'single_node':
     
$node_content = $variables['page']['content']['system_main']['nodes'];
     
$keywords = '';
     
// print '<pre>';
      // print_r($node_content);
      // print '</pre>';
       
$i = 0;
        foreach(
$node_content as $content_array){
          if(
$i == 0){

         
// Get the content body remove html tags and trim to 95 characters.
          // TODO: look first if there is a summary
         
$default_description = substr(strip_tags($content_array['body'][0]['#markup']), 0, 95);

           
// This suppose you created a custom field with the ID 'field_metadescription' in your content type
           
if(is_array($content_array) and isset($content_array['field_metadescription']['#items'][0]['safe_value'])){
             
$content_description = $content_array['field_metadescription']['#items'][0]['safe_value'];
            } else{
             
$content_description = $default_description;
            }
          }
         
          if(
$i == 0 and isset($content_array['field_tags']['#object']->field_tags['und'][0]['taxonomy_term']->name)){
            foreach(
$content_array['field_tags']['#object']->field_tags['und'] as $tag){
             
$keywords .= $tag['taxonomy_term']->name . ',';
            }
           
$keywords .= $default_keywords;
          }
         
$i++;
        }
        break;
    case
'view':
     
$view_title = $variables['page']['#contextual_links']['views_ui'][1][0];
     
$page_number = $variables['page']['#views_contextual_links_info']['views_ui']['view']->query->pager->current_page + 1;

     
$content_description = $view_title;

     
// Add page number for pagined views
     
if ($page_number > 1) {
       
$content_description .= ' page ' . $page_number;
      }
     
$keywords = $default_keywords;
      break;
    default :
     
$content_description = $default_description;
     
$keywords = $default_keywords;
  }

 
// We get the site name
 
if(isset($variables['head_title_array']['name'])){
   
$site_name = $variables['head_title_array']['name'];
  } else{
   
$site_name = t('IConsulting');
  }

 
// We get the page title
 
if(isset($variables['head_title_array']['title'])){
   
$page_title = $variables['head_title_array']['title'];
  }
  else {
   
$page_title = $default_site_name;
  }

 
//We concatain description, site name and page title
 
$variables['meta_description'] = $site_name . ' | ' $page_title . '. ' . $content_description;
 
$variables['meta_keywords'] = strtolower($keywords);
}
?>

html.tpl.php

Finally call your variable in your html.tpl.php file in the head section of your document.

<meta name="description" content="<?php print $variables['meta_description']; ?>" />
<meta name="keywords" content="<?php print $variables['meta_keywords']; ?>" />