Undefined object error from tokens.inc

Created on 10 March 2023, over 1 year ago
Updated 14 March 2023, over 1 year ago

Problem/Motivation

The tokens undefined object error shows up on the entity page that provides a field of type languagefield.

Notice: Trying to get property 'value' of non-object in languagefield_tokens() (line 120 of ../languagefield/languagefield.tokens.inc).

Steps to reproduce

  1. Add a field of type languagefield for a node with field setting allowing the user to choose languages from what's installed on the site already.
  2. Create a node and choose some language for the field of type languagefield and publish the node .
  3. View the node page and you will see the error as above
🐛 Bug report
Status

Fixed

Version

1.9

Component

Code

Created by

fathima.asmat London, UK

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.

  • Issue created by @fathima.asmat
  • Status changed to Needs review over 1 year ago
  • 🇳🇱Netherlands johnv

    I cannot reproduce the problem.

    I guess you have also installed Token module, and some module to add tokens in the view mode, like Automatic Entity Labels?

  • 🇳🇱Netherlands johnv

    Hmm,
    I have committed the patch, since it does not hurt.
    But there must be something else wrong in your installation. The same code is used in other fields for Token support.
    You state it happens with new node, after adding the new field.
    Please check if you are not mixing [node:field_language:langcode] and [node:language:langcode]

  • fathima.asmat London, UK

    Thanks @johnv for merging the fix.

    I guess you have also installed Token module, and some module to add tokens in the view mode, like Automatic Entity Labels?

    I haven't got the token module installed. If this is the case, shouldn't the module to declare the token and the other contrib module as dependencies from the info file and install them as well during the module installation?

    The following bit of code checks the token module service to grab the token type and if not available the module skips from setting up the tokens from the token_info hook.

     $token_type = \Drupal::service('token.entity_mapper')->getTokenTypeForEntityType($entity_type_id);
        if (empty($token_type)) {
          continue;
        }

    This seems like token is a strict dependency for this module, hence should be defined in info.yml as a dependency, perhaps this needs to be created as a separate issue?

  • Status changed to Fixed over 1 year ago
  • 🇳🇱Netherlands johnv

    Token module is not required for this module to work properly.
    It is/was however my impression that the hook_tokens was only called by the Token module (Which is not, it is a core hook: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Utility%2... )

    I still cannot reproduce your problem.
    Upon saving and displaying a node, you need some extra step in the test script in order to make it reproducable.
    It seems you installation is requesting for delta values that do not exist.
    What is the cardinality of your field?

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024