How to add Open Graph Tags in Custom Module detail page? For Node I already used https://www.drupal.org/project/metatag

For my custom module's detail page would like to add Open Graph tags for FB share.

So in my_test.module

function template_preprocess_my_detail_page(&amp;$variables) {

  $items = $variables['item'];

  $title =  $items-&gt;title;

  $og_title = [
        '#tag' =&gt; 'meta',
        '#attributes' =&gt; [
          'property' =&gt; 'og:title',
          'content' =&gt; $title,
        ],
      ];

  $variables['page']['#attached']['html_head'][] = [$og_title, 'og_title'];
}


Above one suppose to work but NOT WORKING.

function MY_MODULE_page_attachments_alter(array &amp;$page) {

  $og_title = [
        '#tag' =&gt; 'meta',
        '#attributes' =&gt; [
          'property' =&gt; 'og:title',
          'content' =&gt; '',
        ],
      ];


  $page['#attached']['html_head'][] = [$og_title, 'og_title'];
}


Above one is working, But I'm not able to get Page Variables. How will I get $variables in above function?

This one called in all pages of module, but I want to call only on detail page as mentioned in proprocess function
I think that you need this.

    /**
 * Alter the metatags for pages that are not of content entities.
 *
 * @param array $metatags
 *   The special metatags to be added to the page.
 * @param array $context
 *   The context, containing the entity used for token replacements.
 */
function hook_metatags_alter(array &amp;$metatags, array $context) {
  // Exclude metatags on frontpage.
  if (\Drupal::service('path.matcher')-&gt;isFrontPage()) {
    $metatags = NULL;
  }
}