I created a basic plain text field on the user entity/bundle via admin/config/people/accounts/fields. I cleared my caches and token cache and there are no tokens exposed for the 'user' token type.

Comments

dave reid’s picture

Ah, the problem is when the text field can have multiple values. Then it gets no token. Otherwise if it's limited to one value for cardinality, then it gets a token. Not sure if this is by design or not.

dave reid’s picture

Title: Entity tokens not created for text fields on user entity » Entity tokens not created for multi-value text fields
fago’s picture

StatusFileSize
new4.2 KB

Indeed, tokens for lists are currently missing. I took a stab on that.
Attached patch implements the tokens using the list<$type> notation as used else too + improves the code to the recently added label() method, via which also list values ala "value1, value2, value3" work.
E.g. the replacement in "title [node:title], All values: [node:field-tags] - a single value: [node:field-tags:1]" would work.

However, that way the code just goes and replace values for each "list" data type, what would pose problems if the token module would like to do it too (see #1047740: Add an [array:*] token type and a [user:roles] token). Maybe we could use the alter hook + add a check whether the token has been already replaced there?

Patch attached.

fago’s picture

Status: Active » Needs review
fago’s picture

The generic list/array handling code should probably just go into the token module, though I guess we need something like the list<$type> notation so we have knowledge of the contained item type.

Status: Needs review » Needs work

The last submitted patch, entity_list_tokens.patch, failed testing.

garethsprice’s picture

Issue tags: +taxonomy, +pathauto, +multiple

This issue also appears when exposing tokens on Taxonomy Reference fields where more than one item can be selected.

fago’s picture

Title: Entity tokens not created for multi-value text fields » Entity tokens not created for multi-value fields
Component: Code - misc » Entity tokens
wjaspers’s picture

subbing.

TimelessDomain’s picture

patch #3 works for taxonomy reference fields.

Jiri Volf’s picture

subscribing

Hamid.D’s picture

subscribing

dasjo’s picture

patch doesn't apply correctly anymore

Hunk #1 succeeded at 33 (offset -1 lines).
Hunk #2 succeeded at 62 (offset -1 lines).
Hunk #3 succeeded at 123 (offset -1 lines).
Hunk #4 FAILED at 155.
Hunk #5 succeeded at 169 (offset -6 lines).
1 out of 5 hunks FAILED -- saving rejects to file entity_token.tokens.inc.rej

it seems the functionality at hunk #4 already got in by annother commit.

anyways, it works for me such that i can now use tokens for multi-terms in rules

dasjo’s picture

Status: Needs work » Needs review
StatusFileSize
new3.1 KB

re-rolled the patch as stated above

casey’s picture

sub

BeaPower’s picture

Component: Entity tokens » Core integration

sub

Status: Needs review » Needs work
Issue tags: -taxonomy, -pathauto, -multiple

The last submitted patch, entity_list_tokens_2.patch, failed testing.

fago’s picture

Status: Needs work » Needs review
Issue tags: +taxonomy, +pathauto, +multiple

#14: entity_list_tokens_2.patch queued for re-testing.

muschpusch’s picture

Could this be related to: #1088538: OG (Organic Groups) tokens missing. ?

[edit]
To be a bit more specific: the og reference field (groups audience) is multi value list.
[/edit]

fago’s picture

yes

Status: Needs review » Needs work

The last submitted patch, entity_list_tokens_2.patch, failed testing.

fago’s picture

Status: Needs work » Needs review
Issue tags: +Needs tests
StatusFileSize
new14.58 KB

finally came back to this one. In the meanwhile token.module got its 'array' type, but that doesn't fit for our list types as those don't have array keys - nor would it play with chaining list-item tokens.

Thus, attached patch adds token types for the entity api list types, i.e. list<TYPE> + implements token values for those lists. I also had a look at fixing #1183676: improve support for arbitrary data structures, though for a proper fix more work is needed (see issue). It basically works though: Tokens in data structures work, but are already specified as *dynamic* token and list possible properties in the help text.

Attached patch works for me, e.g. using this test:
echo token_replace('[node:field-tags] - [node:field-tags:0] - [node:field-tags:1:url] - [node:field-image:alt] - [node:field-image:file:url] - [site:current-page:url]', array('node' => $node));

We'll need test for that though.

dave reid’s picture

The array token type does have array keys, but does not yet support chaining. There is an open issue to add support for this in a way which will make the token UI allow those tokens to be listed in help as well: #1195874: Need to figure out how to create nested tokens from the array token type.

karens’s picture

I used the patch in #22 and tried to use [node:field-event-date:value2] in a pathauto token and it seems to work. Then I tried [node:field-event-date:value2:medium] and it gave me the error Fatal error: Call to undefined method EntityValueWrapper::language() in /../entity/entity_token.tokens.inc on line 268. Maybe the second wasn't expected to work yet.

So at this point I can set a token using Entity Tokens based on either value or value2 but I can't control its format.

fago’s picture

StatusFileSize
new14.68 KB

@array-keys:
Yep, I know the 'array' type supports them. The entity api 'list' type is different though: it is defined as "list of values", thus it does not support the concept array keys. Still, those lists are represented as numerically indexed arrays, but they are by definition not like PHP arrays.

They 'array' type would perhaps match more for "entity api data structures", see #1183676: improve support for arbitrary data structures. Are there any plans on adding support for sub-tokens per token to token UI? I guess, best that would be implemented by additional data-type/token-type anyway. Maybe those should be hidden from the complete token UI listing though.

@Karen: Thanks for testing. Also the second use-case should work, you found a bug though. Attached patch should fix it, please test.

karens’s picture

With that patch, trying to add a format after 'value' or 'value2' results in this error:

Fatal error: Call to a member function label() on a non-object in ../entity/entity_token.tokens.inc on line 285

fago’s picture

StatusFileSize
new14.68 KB

ok, I gave this a test too. Figured out there was still in issue with chaining in structs - attached patch fixes that. This certainly needs proper test-coverage ;)

karens’s picture

Yay! that one works for me, at least for date tokens. Don't know what else to test.

mrfelton’s picture

Status: Needs review » Needs work

I tried that patch in #27 trying to allow me to chain tokens through a Drupal Commerce product reference field to access properties of taxonomy terms associated with the related Drupal commerce product entity. After applying the patch I see a new line in the placeholder tokens fieldset:

Product	[node:field-product:?]	Field "field_product". The following properties may be appended to the token: product_id (Product ID)

So I tried a token of [node:field-product:product_id]. This resulted in a fatal eror:

Fatal error: Call to a member function label() on a non-object in /var/aegir/platforms/dev/example-7.9-20112011/sites/all/modules/contrib/entity/entity_token.tokens.inc on line 285

Also, I was hoping to be able to get more from the related product entity than just the product id, such as the taxonomy terms associated with it. Will this patch eventually allow for deeper chaining, or would that need t be handled elsewhere, and if so, where? What is it that determines that only the product_id field will be accessible?

fago’s picture

Status: Needs work » Fixed

Thanks mrfelton for testing - I had a short look at it and noted that's caused by some incorrect property-info. See #1356006: Fix entity property info of product / customer profile / line item reference fields. With the patch over there it worked fine for me.

Apart from that, I've implemented tests, fixed tokens for not existing items to be correctly ignored and added an improvement to make use of token module's cached token info if available. Committed.

Status: Fixed » Closed (fixed)

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

Hendry Lee’s picture

I tested this again with link field.

[node:field-link:0] gave the following result:

PHP Fatal error: Call to a member function value() on a non-object in /home/dev/www/sandbox.dev/sites/all/modules/contrib/entity/entity_token.tokens.inc on line 297

Hendry Lee’s picture

Status: Closed (fixed) » Needs work

If I entered [node:field-link], this is what I got on screen:

Property 0, Property 1

[node:field_link] displays the content right.

fago’s picture

Status: Needs work » Closed (fixed)

Please open a new issue for any follow-up problems.

crimsondryad’s picture

We need this, any chance of a release anytime soon?

nguyentran’s picture

Hello, i have the same problem as #33, i have tested with [node:field-link:0], [node:field-link:1], it does not work. Any help to solve this.
Thanks.

joshuautley’s picture

Priority: Normal » Major
Status: Closed (fixed) » Active

We're working on a related solution due to a need to specify which value in a token array to display. Tried this as described with no luck.

An array with three values produces "Property 0, Property 1, Property 2"

Another thread is also having the same result > http://drupal.org/node/1440928

I'll see what the maintainer of the token module has to say.

mitchell’s picture

Priority: Major » Normal
Status: Active » Closed (fixed)

Please open a new issue for any follow-up problems.

#33, #36, #37: Please follow up on this in #1440928: Entity tokens for multi-value fields produce a fatal error.

mfoda’s picture

Has this issue been resolved in the 7.x1.0-rc3 or only in the current dev version?

klonos’s picture

@mfoda: Hope I'm not too late to reply this question of yours Mohammad. Let's hope that this will be helpful to others too...

Back in #30 fago (the maintainer of Entity API) said that this issue here is supposed to be fixed by the commit in #1356006: Fix entity property info of product / customer profile / line item reference fields. That issue was closed as fixed with this commit. That dates 20 Dec 2011 and the 7.x-1.0 version of Entity API dates 25 Dec 2012, so the main issue should be fixed in 7.x-1.0. In fact -based on the release dates- the fix should actually exist since 7.x-1.0-rc2.

Now, I'm coming from #1103032: Document how to use date tokens where the Entity API is suggested as a solution for this, but I don't know if #1440928: Entity tokens for multi-value fields produce a fatal error makes it still an issue.

markusd1984’s picture

Has anyone been able to use this successfully?

I went even back to Entity 7.x-1.2 with patch #44 yet still not working [event-node:field-datetime:0:value:custom:Y].

I also tried with Entity 7.x-1.9 and dev which seems to have this patch built in but no success, when using it in a rule to send a mail within the body I get no output at all.

Only [event-node:field-datetime] which outputs Wed, 01/04/2020 - 18:00
And [event-node:field_datetime] which outputs 22020-04-01 18:00:00 to 2020-04-01 18:30:00

I want it similar to the way the field is displayed on the node Wednesday, 1 April, 2020 - 18:00 to 18:30

I looked through all issues in date and entity queues to no avail, including https://www.drupal.org/project/entity/issues/1440928.