Get content of referenced fields in multiple value fields

Created on 16 October 2024, 9 months ago

Problem/Motivation

I'm using the metatags module and want to add a default value for the
opengraph standard for the image. The media field in my content type can have
unlimited values, not just one.

For a single value reference field I can use something like

[node:field_media_image:entity:*:url]

to get the url of the image.

But I cannot use this in the same way for multi-value reference fields, like

[node:field_media_image:0:entity:*:url]

This just seem to get ignored.

Am I missing something or why isn't this possible?

Thanks in advance.

💬 Support request
Status

Active

Version

10.4

Component

token system

Created by

🇩🇪Germany droprocker

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

Comments & Activities

  • Issue created by @droprocker
  • 🇮🇳India ajinkya45 Pune

    You are right, I tried the same and I faced the same issue. This behavior due to a limitation in how Drupal's Token system which the Metatag module relies on handles multi-value entity reference fields, especially when those fields reference Media entities that in turn reference File entities (such as images). The token system does not fully support indexed access (:0:) for multi-value fields. Additionally, when dealing with nested entity references, for example: Node → Media field → Media entity → Image field → File entity → URL
    the token resolution tends to fail silently. This is because the token system cannot reliably traverse these complex relationships in a deeply nested, multi-value context. In contrast, simple tokens like [node:field_image:entity:url] work as expected because they involve a single-level, single-value reference, which the token engine can easily resolve.

    To achieve the desired behavior what might work is:
    1. Creating a custom token using hook_token_info() and hook_tokens().
    2. Or, implement a custom preprocess hook in the theme or a custom module to programmatically set the Metatag values, allowing full control over multiple image meta tags.

Production build 0.71.5 2024