If you are familiar with Drupal's caching mechanism you would know that all data in Drupal are cached to their respective cache tables. Like page cache is stored to the cache_page table, menu cache is stored to cache_menu. Similarly, metatag information's are cached in cache_metatag table. In this article, I will show you how to add different metatags for pages with the same URL. In our case, we were checking the value in the query string parameter and setting the metatag description. You can use template_preprocess_html to update the metatags. An example of the template_preprocess_html implementation is given below.
/**
* Implementation of template_preprocess_html().
*/
function MODULE_NAME_preprocess_html(&$variables) {
if (arg(0) == 'test' && isset($_GET['custom']) && $_GET['custom'] == 'xxyyzz') {
$set_description = array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'description',
'content' => 'Custom description.',
),
);
drupal_add_html_head($set_description, 'set_description');
}
}
This helped us change the metatag description of the page depending on the query string parameter. The metatag module caches information by checking the page URL. So only the first page request will load all the metatag information and later all the information is delivered from the cache. So we had to implement hook_metatag_page_cache_cid_parts_alter() to set the cache id for setting metatag cache. An example of the hook_metatag_page_cache_cid_parts_alter implementation is given below
/**
* Implements hook_metatag_page_cache_cid_parts_alter().
*/
function MODULE_NAME_metatag_page_cache_cid_parts_alter(&$cid_parts) {
if (arg(0) == 'test') {
$cid_parts['path'] = substr(request_uri(), '1');
}
}
This way the request_uri will be set as the cid for caching the metatag information. This way we can set our own cid whatever be the parameter that is dynamic on the page. If you are still having trouble setting the metatag talk to a Drupal expert. Please fill in our Contact Us form and we will get back to you shortly.