1

I have some pages which are created by page callback functions. I'm using the metatag module. I'm on D7

I need to control the metatags output (specifically the description/og:description).

This documentation looks like it would have been useful, but is empty: https://www.drupal.org/docs/7/modules/metatag/howto-assign-meta-tags-to-specific-non-entity-pages-d7

Thanks.

1
  • 1
    Did you try Context + Metatag:Context? Metatag:Context allows assigning metatags using various context definitions including paths. Commented Oct 3, 2017 at 15:18

2 Answers 2

0

metatags has its own submodule for this, just enable 'Metatag: Context (metatag_context)'

then it appears in 'Add reaction' as 'Metadata'

0

There's a hook for that. Here's my implementation.

<?php

function mymodule_some_page_callback() {
  global $metatagOverrides;
  // ... generate your page ...

  $metatagOverrides = [
    'og:image' => 'https://example.org/the-image.jpg',
    'og:description' => 'Some description',
    ...
  ];

  return $thePage;
}

function mymodule_metatag_metatags_view_alter(&$output, $instance, $options) {
if (!empty($metatagOverrides)) {
  foreach ($metatagOverrides as $tagname => $value) {
    if (array_key_exists($tagname, $output)) {
      $output[$tagname]["#attached"]["drupal_add_html_head"][0][0]["#value"] = $value;
    }
    else {
      $output[$tagname] = [
        '#attached' => [
          'drupal_add_html_head' => [
            [
              [
                '#theme' => 'metatag_property',
                '#tag' => 'meta',
                '#id' => "{$tagname}_0",
                '#name' => $tagname,
                '#value' => $value,
                '#weight' => 100,
              ],
              "{$tagname}_0",
            ]
          ]
        ]
      ];
    }
  }
}

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.