0

Is there a way to retrieve an arbitrary user entity via tokens inside a node field?

For example:

Let's say I have a field name Source, which contains a reference to an external news publication.

I have a user on my site named "Jane Doe".

I want to be able to put something like this in the source field. The example is a little fudged b/c I don't think Tokens has a "link" type token, but the question is more about getting data for an arbitrary user:

New York Times, citing [user:link:Jane Doe].

And have that rendered as

New York Times, citing Jane Doe.

What I have tried

I've attempted this in the node body field (using the token_filter module).

I can get data for the current user with, such as with the token [current-user:url].

But I haven't figured out how to get data for an arbitrary user.

I've tried:

  • [user:url:1]
  • [user:url 1]
  • Tokens work the other way round - they get given a context to work with (node/user/taxonomy term/whatever), they don’t take part in loading/preparing that context. You might need to write a custom filter for this – Clive Dec 20 '18 at 7:47
  • This is right, core has two user tokens, current-user for the global context and user for the local user. The second one works locally in a user field, but not in a node field, because then the local entity is a node. So you would need to define a third type of user token in a custom module which gets the fields of an arbitrary user, which is no problem to be used globally then, because it doesn't depend on local data. – 4k4 Dec 20 '18 at 7:48
0

There isn't a token that, given a user ID, would return data about that user account. current-user returns data about the currently logged-in user, while user returns data about the user account passed to the function (in Drupal 7, or the method in Drupal 8) that replaces tokens with their values: token_replace().

It would be possible, for a module, to implement a token that returns data about every user accounts. With such a module, for example, users:1:name could return the username for the user account #1, while users:2:name would do the same for the user #2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.