I am using the metatag module for defining several meta tags. As a default value, canonical tags use '[current-page:url:absolute]'. Now I want to create a module that sets the value of the canonical tag to some other url.

Setting $node['metatags']['und']['canonical']['value'] to another string doesn't seem to work. Unfortunately, I also can't use an entity_wrapper, since metatag doesn't define information for its custom fields, which is needed by entity_wrapper.

Is there something else I could do?

  • Are you using the metatag hooks? Jan 12, 2015 at 13:10
  • No, I don't. Do you have any information about these hooks?
    – pvorb
    Jan 12, 2015 at 13:28

2 Answers 2


look into the hook: metatag_metatags_view_alter().

 * Implements metatag_metatags_view_alter()
function mymodule_metatag_metatags_view_alter(&$output, $instance) {
  // Add Custom logic to overwrite the canonical tag here.
  $canonical = 'http://www.mysourceurl.com';

  // Alter canonical tag.
  $output['canonical']['#attached']['drupal_add_html_head'][0][0]['#value'] = $canonical;
  • thanks this helped. but i need to call it mymodule_metatag_metatags_view_alter, not mymodule_canonical_metatag_metatags_view_alter
    – manishie
    Jul 8, 2015 at 8:20
  • My module was called mymodule_canonical but I can see that it is confusing so I updated it to mymodule.
    – albertski
    Mar 31, 2017 at 20:46

The metatag module provides server hooks. You can find them in the metatag.api.php file inside the module.

The one you need is hook_metatag_metatags_view_alter.

 * Alter metatags before being cached.
 * This hook is invoked prior to the meta tags for a given page are cached.
 * @param array $output
 *   All of the meta tags to be output for this page in their raw format. This
 *   is a heavily nested array.
 * @param string $instance
 *   An identifier for the current page's page type, typically a combination
 *   of the entity name and bundle name, e.g. "node:story".
 * @param array $options
 *   All of the options used to generate the meta tags.
function hook_metatag_metatags_view_alter(&$output, $instance, $options) {
  if (isset($output['description']['#attached']['drupal_add_html_head'][0][0]['#value'])) {
    $output['description']['#attached']['drupal_add_html_head'][0][0]['#value'] = 'O rly?';
  • Somehow this is not what I want. I try to do something similar to changing the canonical manually when adding a node. Is there a way to do this?
    – pvorb
    Jan 16, 2015 at 10:42
  • I'm not really following. Do you want to change the canonical tag or another tag? Maybe drupal.org/project/metatags_quick is closer to what you need? Jan 16, 2015 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.