4

I need to add a custom metatag to D7 site, something like here: http://groups.drupal.org/node/83069. What are the possible options? I know there is a metatag module, but seems it doesn't support custom tags.

I could modify the theme, but I need some user configurable solution.

1
  • You can have a look at drupal_add_html_head, but you'll need to write a custom module to make it configurable. – Attiks Oct 19 '11 at 19:00
2

Did you have a look at Meta tags quick?

There is a comparison page for meta tag generation modules.

2
  • Thanks for an idea. I found that path based metatags don't seem to work with clean urls, but it was really easy to add needed tags by editing known_tags.inc. – Dmitry Vyal Oct 20 '11 at 10:11
  • 1
    btw, if you really have to modify the module and can't override the known tags in your own custom module, remember to write patch and apply it once you upgrade, if not you'll loose your custom tags. Glad I could help :) – ipwa Oct 21 '11 at 0:02
12

Metatag does support custom tags by implementing hook_metatag_metatags_view_alter(). See the following hook implementation to add the Open Graph video:duration metatag from a custom entity:

/**
 * 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".
 */
function mycustommodule_metatag_metatags_view_alter(&$output, $instance) {
  if ($instance == 'mycustom:entity') {
    $entity = mycustomentity_load(someid);
    $duration = $entity->duration;
    $output['video:duration']['#attached']['drupal_add_html_head'][0] = array(
      array(
        '#theme' => 'metatag_opengraph',
        '#tag' => 'meta',
        '#id' => 'video:duration',
        '#name' => 'video:duration',
        '#value' => $duration,
      ),
      'video:duration',
    );
  }
}
5

Replying late but this may be helpful for others:

I have written about my solution here http://mymixerone.blogspot.in/2012/11/adding-meta-tags-in-drupal-node.html

Just have a look if its helpful.

Here is the solution in brief, Try adding this in your templet.php file:

<?php
function <theme-name>_preprocess_html(&$variables) {
  switch(arg(1)) {
    //current-issues, node id 33
    case "33" :
      // title
      $variables['head_title']= "New Title";

      //key-words
      $page_keywords = array(
        '#type' => 'html_tag',
        '#tag' => 'meta',
        '#attributes' => array(
          'name' => 'keywords',
          'content' => 'all keywords for this node',
        )
      );
      drupal_add_html_head($page_keywords, 'page_keywords');
      break;

    //node id 20
    case "20" :
      // title
      $variables['head_title']= "New Title";
      break;
  }
}
?>

other tags can be added similarly.

3
  • Brief your answer here that will help a lot. – Mathankumar Nov 30 '12 at 19:51
  • so basically you're hard-coding metatags into the template? Seems to be a rather dirty way to me. Moreover, in my case the client wished to be able to edit tags, so this approach wasn't feasible. – Dmitry Vyal Dec 3 '12 at 11:55
  • Its not dirty as this is done in sub folder of theme. And Intended for someone who needs more control. besides you can always create a form for your client, Save input in a new table and then fetch Tags from there If u do not wish to hardcode :) – user11856 Dec 4 '12 at 17:47
3

Thank you Atul. I've used your approach to make refreshing page in Drupal support module every 30 seconds.

function garland_preprocess_html(&$vars) {
//Adding Refresh to main tikets page BEGIN
//print 'Node id = ';print(arg(1));
switch(arg(1)){
//check node id : if nid=kontora then use custom "Refresh setup" (must be the name of client you want to refresh)
case "client1" :
/*Implementation of HTML equivalent <meta http-equiv="Refresh" content="30" />*/
$http_equiv = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array(
    'http-equiv' => 'Refresh',
    'content' => '30',
    )
  );
drupal_add_html_head($http_equiv, 'http_equiv');
// Meta tag code here
break;
/*
//node id 20
case "20" :
      // Meta tag code here
break;*/
     }
//Adding Refresh to main tikets page END
}
0

You can also implement hook_metatag_info in a custom module, define your metatag and then you can configure it with UI.

You can see this implementation from metatag module, by example http://www.drupalcontrib.org/api/drupal/contributions%21metatag%21metatag_opengraph%21metatag_opengraph.metatag.inc/function/metatag_opengraph_metatag_info/7

-1

Custom meta module will do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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