Can't find the right token (URL) for og:image in Drupal 10

Created on 11 September 2023, over 1 year ago

Problem/Motivation

My setup is: Drupal 10.1.3 and Metatag 2.0.0

In all of my content types, I have the same image field:
Label: Image
Machine name: field_media_image
Field type: Entity reference
Reference type: Media
Media type: Image

I'm trying to add og:image metatag to my pages for all content types.

So I've enabled 'Metatag: Open Graph' submodule and proceeded to:
admin/config/search/metatag/node

In 'Open Graph' section I've tried these combinations (both with Image and Image URL):

[node:field_media_image]
The result is:
<meta property="og:image" content="https://site.commy_image.jpg">

[node:field_media_image:entity:url]
The result is:
<meta property="og:image" content="https://site.com/media/23/edit">

[node:field_media_image:entity:field_media_image:url]
The result is:
<meta property="og:image" content="">

Which token should I use to make it look:
<meta property="og:image" content="https://site.com/sites/default/files/2023_09/image_name.jpg">

πŸ’¬ Support request
Status

Active

Version

2.0

Component

Open Graph

Created by

πŸ‡¬πŸ‡ͺGeorgia almador

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @almador
  • πŸ‡¬πŸ‡ͺGeorgia almador

    It looks like providing:
    [node:field_media_image:entity:field_media_image]
    at 'Advanced -> Image' and 'Open Graph -> Image' did the trick!

  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    Thanks for working through it and finding the right solution.

    I'm going to turn this into a documentation task to update the module's docs with suggestions on which tokens to use.

  • πŸ‡¬πŸ‡ͺGeorgia almador

    Thanks, Damien!

    I've read somewhere that I need to enable 'Custom display settings' for 'Token' view mode at:
    admin/structure/types/manage/content_type/display

    Is it necessary?

  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    You only need to enable that view mode if you specifically need to customize the output of one of the tokens, so it isn't strictly necessary.

  • πŸ‡³πŸ‡΄Norway vegardjo

    Chiming in as this was the first result on Google.

    When I do the equivalent of [node:field_mediaref_on_node:entity:field_media_image] I get several og:image meta tags, one for the URL, another for the title, another for the caption etc.

    If I do [node:field_mediaref_on_node:entity:field_media_image:url] I get no og:image tag.

    Via this documenttation https://www.drupal.org/docs/contributed-modules/metatag/frequently-asked... β†’ I found that you should include the image style machine name, like so:

    [node:field_mediaref_on_node:entity:field_media_image:wide:url]

    ..here "wide" is the image style, and this gives me correct content in the og:image tag.

  • πŸ‡©πŸ‡°Denmark Steven Snedker

    You are absolutely right, @vegardjo. I've posted a better description for the og:image field as a feature request at More helpful description for the og:image field ✨ More helpful description for the og:image field Active .

Production build 0.71.5 2024