[Drupal] Meta tag is not showing when using custom page template files

| | 1 min read

In one of my Drupal projects, I was using metatag module to specify metatags in my entity. When I enabled the module in Drupal modules configurations and set metatgs in my contents, it was working perfectly. After some days I want to customize my content page, so I created a custom page template file for the content. Then onwards, in the Drupal site the metatgs I set is not showing in the content page.

I checked for the metatags configurations in Drupal settings to confirm whether there is anything got wrong in the configuration. Then I debugged in the metatag module and at last I found that, the metatag module is adding the metatags to the $page['content'] on execution of hook_page_build. So the metatgs will print only if $page['content'] or $page['content']['metatags'] is rendered in the template file.

This is already specified as known issues in the README file of metatags module in Drupal as follows,

When using custom page template files in Drupal , e.g., page--front.tpl.php, it is important to ensure that the following code is present in the template file:

    <?php render($page['content']); ?>

  or

    <?php render($page['content']['metatags']); ?>

Without one of these being present the meta tags will not be displayed.

A tip here is, if you got into a trouble with a module in Drupal, first check the README file of the module and its code before searching outside.