Fails when field is part of a referenced entity

Created on 25 August 2014, over 10 years ago
Updated 16 January 2025, about 1 month ago

So in the _token_filter_filter_tokens function, there is a break; called when the backtrace loop finds a call from field_default_view. This is done via a switch. Inside the switch statement, the entity that was part of the original call is loaded so that the tokens can be properly replaced.

This doesn't work correctly when the entity, that has the token, is being rendered via a parent entity with an entity reference. The backtrace foreach loop ends up finding the field_default_view for the parent entity and loads that one up for token replacement.

I think this can easily be fixed by just replacing

          case 'field_default_view':
        // field_default_view($entity_type, $entity) is fairly reliable since
        // it is called by both field_attach_view() and field_view_field().
        $entity_type = $caller['args'][0];
        $entity = $caller['args'][1];
        $token_type = token_get_entity_mapping('entity', $entity_type);
        $data[$token_type] = $entity;
        // Use the proper language code that field_default_view() was called with.
        if ($langcode = $caller['args'][4]) {
          $language_list = language_list();
          if (!empty($language_list[$langcode])) {
            $options['language'] = $language_list[$langcode];
          }
        }
        break;

with

     case 'field_default_view':
        // field_default_view($entity_type, $entity) is fairly reliable since
        // it is called by both field_attach_view() and field_view_field().
        $entity_type = $caller['args'][0];
        $entity = $caller['args'][1];
        $token_type = token_get_entity_mapping('entity', $entity_type);
        $data[$token_type] = $entity;
        // Use the proper language code that field_default_view() was called with.
        if ($langcode = $caller['args'][4]) {
          $language_list = language_list();
          if (!empty($language_list[$langcode])) {
            $options['language'] = $language_list[$langcode];
          }
        }
        break 2;

I haven't done any extensive testing, but I can't see the drawback of breaking out of both the switch and the foreach as soon as the first field_default_view caller is found.

πŸ› Bug report
Status

Closed: outdated

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States JonMcL Brooklyn, NY

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024