Drupal and Meta Tags

Kiam's picture

Meta tags are important in a web site; they permit the webmaster to improve the volume and quality of traffic to a web site from search engines, to target the search for specific keywords.

If you would need to add them to a Drupal 6 powered web site, you could use one of the following:

  • Meta tags has been published first, and has releases for Drupal 4.6, 4.7, 5.x, and 6.x. It permits to associate metadata to taxonomies, nodes, and it integrates with panels.module, and views.module (even if the last feature has been disabled, waiting for a stable version of view.module, and a porting to Drupal 6 of panels.module). It's the first module I installed, but I uninstalled it when Google Webmaster Tools signaled some problems with the meta tags it added; examining the source code, I noticed it was not completely following what the Drupal API documentation was reporting for the value returned by an implementation of hook_nodeapi() when the parameter $op is passed with the value 'load'.
  • Integrated Metatags is more recent, and has releases just for Drupal 5.x, and 6.x. It allows to decide which node fields should appear like meta tags, to configure them on a per-content type basis, to support content.module fields, and to combine fields content into a single meta tag. The features are interesting, but I was not able to use the module on my web site because of some problems the module has (or because some compatibility issues with the other modules I am using).

The module I wish to use for my web site should have these features:

  • It should create meta tags only for nodes; that is what the content of a Drupal powered site is made of, and that is what should have any metadata associated. To associate any metadata directly with the nodes would also simplify how to generate them.
  • It should permit to associate any meta tags to the content of the node fields.
  • It should be integrated with the content.module.
  • It should permit to the administrator of a web site (or any user with the right permission) to decide which meta tags to use, in which order, and to which node fields associate it.

All this would be possible in Drupal 6.

  • The function menu_get_object() returns the object associated with current path Drupal is showing; by default it returns the node object whose ID is the number returned by arg(1).
  • token.module allows to replace some placeholder with values taken by nodes fields, by user properties, or global variables; it is integrated with content.module.
  • content.module allows to select which fields can be visible when the node is being viewed, and content_permissions.module (a core module in Drupal 6) allows to select which roles are able to edit, or view a CCK field.

Such a module could implement hook_init() in this way:

/**
 * Implementation of hook_init().
 */
function metadata_init() {
  if ($node = menu_get_object()) {
    $type_url = str_replace('_', '-', $node->type);
    $metadata = metadata_get_items(array('type' => $type_url), array('weight' => 'ASC'));
 
    foreach ($metadata as $id => $metadatum) {
      if (($content = $metadatum['content']) != '') {
        $content = token_replace_multiple($content, array('global' => NULL, 'node' => $node));
        drupal_set_html_head('<meta name="'. $metadatum['name'] .'" content="'. check_plain($content) .'" />');
      }
    }
  }
}

and hook_menu() like:

/**
 * Implementation of hook_menu().
 */
function metadata_menu() {
  foreach (node_get_types('types', NULL, TRUE) as $type) {
    $type_url = str_replace('_', '-', $type->type);
 
    $items["admin/content/node-type/{$type_url}/metadata"] = array(
      'title'            => 'Metadata',
      'page callback'    => 'drupal_get_form',
      'page arguments'   => array('metadata_overview', 3),
      'access arguments' => array('administer metadata'),
      'weight'           => 5,
      'type'             => MENU_LOCAL_TASK,
      'file'             => 'metadata.admin.inc',
    );
    $items["admin/content/node-type/{$type_url}/metadata/list"] = array(
      'title'            => 'List',
      'page callback'    => 'drupal_get_form',
      'page arguments'   => array('metadata_overview', 3),
      'access arguments' => array('administer metadata'),
      'weight'           => -1,
      'type'             => MENU_DEFAULT_LOCAL_TASK,
      'file'             => 'metadata.admin.inc',
    );
    $items["admin/content/node-type/{$type_url}/metadata/add"] = array(
      'title'            => 'Add metadata',
      'page callback'    => 'drupal_get_form',
      'page arguments'   => array('metadata_edit_form', 3),
      'access arguments' => array('administer metadata'),
      'type'             => MENU_LOCAL_TASK,
      'file'             => 'metadata.admin.inc',
    );
  }
 
  return $items;
}

metadata_get_items() would return the list of the meta tags filtered by node types, metadata_overview() would list the metatags defined for the node type, and metadata_edit_form() would present the form to edit the meta tags definition.

Maybe one day I will submit such module to Drupal.

Your rating: None

Comments

Re: Drupal and Metatags

For completeness of information, int_meta.module doesn't work on PHP4 because it uses a function that is only present on PHP5 or higher.

n/a

Re: Drupal and Metatags

Rather than to implement hook_init(), it would be preferable to implement hook_nodeapi() which would be garanteed to be called for any nodes (cached or not).
In that case, the call to menu_get_object() would be superfluous.

n/a

Re: Drupal and Metatags

Integrated Metatags doesn't make any references to PHP5 only functions; actually, it's the only meta tags module which has been updated in the last two months (Meta tags has not been updated since February 15, when the 6.x-1.0-rc1 version has been released).
Comparing the two modules, Integrated Metatags seems more flexible as it allows to create meta tags from the content of one or more CCK fields, which can be combined to create a single meta tag; it doesn't use a fixed list of meta tags, and that makes the module more suitable whenever there is the need to integrate custom metadata in the Drupal output.

At the moment, Meta tags is the more downloaded module: its 6.x-1.0-rc1 usage statistics shows a peak of 6906 on October 5, while the Integrated Metatags project reaches a 123 in the same day (and a peak of 494 on September 28).

n/a