Use tokens for Data comparison

Created on 4 May 2023, almost 2 years ago
Updated 12 May 2023, almost 2 years ago

Problem/Motivation

I've tried to use tokens in Data or Text comparison condition, so I can compare token value with fixed value, but it's not possible.

I've got the error:

> Data selector [current-user:uid] for context Data to compare is invalid. Unable to get variable '[current-user:uid]'; it is not defined.

Steps to reproduce

1. Create a custom rule.
1. Go to: /admin/config/workflow/rules/reactions/edit/%/add/rules_condition
1. Type [current-user:uid] into DATA TO COMPARE field.

Or maybe there is another way to compare token values?

πŸ’¬ Support request
Status

Fixed

Version

3.0

Component

Rules Core

Created by

πŸ‡¬πŸ‡§United Kingdom kenorb

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

Comments & Activities

  • Issue created by @kenorb
  • Status changed to Fixed almost 2 years ago
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    For data comparisons using the "Data comparison" condition, the "Data to compare" is chosen using the Data selector, which is the default and only way to choose the data item you want to compare to. Clicking inside the input box will give you the top-level list of available objects in context, then you can drill down into specific parts of those objects.

    If your event provides a "user" context variable, then you would enter something like user.uid or account.uid. If there is no user object in context, you can use the global user object provided by the core user module like @user.current_user_context:current_user.uid.

    You enter "tokens" in the "Data value" textfield using "Direct input" mode, which is the default for that textfield. Rules does not use core Token module tokens in D8 and higher. Instead, it uses Twig-like tokens. So something like {{ user.uid }} or {{ account.uid }} or {{ @user.current_user_context:current_user.uid }} should work. The available tokens for use here are all the same as you will find in the data selector described above, but surrounded by {{ and }} so you may embed them in text strings. Rules also has the concept of data filters (again, like Twig), which act on these "tokens" to manipulate the data. For example, {{ user.name | lower }} will apply the "lower" data filter to the user name in order to convert it to all lowercase.

    There is documentation for this at https://www.drupal.org/docs/contributed-modules/rules-essentials/token-r... β†’ and in the other pages of that guide. If you find something wrong or missing in that documentation please either correct the documentation (you can do this yourself) or add a comment or add a new documentation page.

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

Production build 0.71.5 2024