Problem/Motivation

Text field tokens appear in the token browser list when the type "node" is selected, but then during token_replace(), they are not generated by token_tokens() or field_tokens().

Example: I have a text field called "photo credit", and it appears in the token list as [node:field_photo_credit]. But when I use this token, it is not getting a value in token_replace().

Proposed resolution

Fix field_tokens() so that it generates field token replacements for nodes and other entities. I think the problem is here (about 3 lines down in the function):

  if ($type == 'entity' && !empty($data['entity_type']) && !empty($data['entity']) && !empty($data['token_type'])) {

This is only generating replacements for $type == 'entity', but if the token name is "node:" then $type == 'node'.

Remaining tasks

See summary.

User interface changes

None.

API changes

None.

Original report by jhodgdon

I'm running into kind of an odd problem that I just spent about an hour attempting to debug, and still failed.... This is for a site that someone else set up and I inherited. Here's the situation (Drupal 7.x, Colorbox 7.x-1.4, Token 7.x-1.4)

- We have a content type "Bird", with a nodereference field "Related Images", which contains one or more nodes of content type "Photo".
- Content type Photo has an Image field on it (field_image) and a Text field (field_photo_credit).
- There is a View that is displayed as a block on Bird pages, which is using colorbox to display the related images. It has a relationship to the Photos (via the nodereference field), and then field_image is chosen as a Field in the View.
- The formatter for the field is set to "Colorbox", and we want to display a custom caption that says "(photo node title) by (photo credit)".

So here's the problem -- there are two:

a) In the Views field formatter settings, when I choose a "custom" caption, the token replacements listed for the custom caption do not list any node-related tokens (only site-generic and File tokens are listed). In debugging, I found out that Colorbox thinks for some reason that it should get a list of all "ctools" and "file" tokens, and since that is what it is telling the Token module, node tokens are not being added to the list. I don't know why that would be... As a note, if I try to set up the Image field on the Photo content type to display with a custom caption, on that token list I get the node tokens -- so it's only within Views that I don't. I have no idea why.

b) From the token list on the Photo content type, I did find the correct token names. So I set up the custom field as "[node:title] by [node:field_photo_credit]", but it doesn't work -- the photo credit spot always comes up blank. I tried debugging this, but I couldn't figure out why this token replacement was not working... It also didn't work when I did this directly on the Photo content type's "Manage Display" screen, using the token selector (which had both of these as options), so it is the same result inside and outside Views.

So is there a way for me to get this to work? I'm not sure if the problem lies in Colorbox, or Token, or in how they are interacting.... Any ideas or help would be much appreciated!!

CommentFileSizeAuthor
#9 token-readme.patch299 bytesjhodgdon
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon’s picture

Title: How to create custom Views caption? » How to create custom caption with text field from the node?
frjo’s picture

Title: How to create custom caption with text field from the node? » How to create custom caption with text field from the node in a view?
Assigned: Unassigned » frjo
Category: support » task

In the formatter settings on the content type it works but when you use the same formatter in a view the entity type is set to "ctools".

If you manually enter e.g. [node:title] it works because in the formatter view the entity type is set to "node" as it should be.

A quick fix is to go to "Manage display" for your content type, set the custom caption with tokens there. Copy it and go to the view and past it in and it should work.

I set this as a task for now to remember to check in on it later.

jhodgdon’s picture

RE #2 - no, that doesn't work. As stated in my original report, I did try the exact same custom caption of "[node:title] by [node:field_photo_credit]" in both the Manage Display settings and the View, and neither one of them displays the photo credit.

frjo’s picture

When I test "[node:title]" it works without problems. Have you confirmed that they work as they should on standard node view?

jhodgdon’s picture

OK, let me try to state this again.

a) [node:title] works fine on both standard node view and Views in Colorbox custom captions.

b) [node:field_photo_credit], which I inserted in the standard Node display into the custom caption for Colorbox (in the Manage Display screen) using the token browser, does not work in Node display. It also doesn't work in Views. The custom caption says "[node:title] by [node:field_photo_credit]" but it always displays things like "Bald Eagle by " (with no photo credit).

Try adding a text field to a content type on your test site and using that in a custom Colorbox caption, and I expect you'll see the same problem.

jhodgdon’s picture

Title: How to create custom caption with text field from the node in a view? » Node tokens for Text fields appear in token list but don't work in token_replace()
Project: Colorbox » Token
Assigned: frjo » Unassigned
Category: task » bug

You know, the more I think about it, the more I think this is a bug in the Token module and not Colorbox... When I was debugging this, I saw that the text field tokens appeared in the token browser list when the type "node" was selected, but then during token_replace(), they were not generated by token_tokens() or field_tokens(). I was thinking it was a bug in how Colorbox was using tokens, but I think it's instead a bug in the Token module.

jhodgdon’s picture

I just edited the issue summary... it could also be a problem in Drupal Core?

Dave Reid’s picture

Status: Active » Postponed (maintainer needs more info)

Can you please check what the field_photo_credit field is set to display both as the 'Default' and 'Token' view mode for the node type? See http://drupal.org/node/1299662 for more information.

jhodgdon’s picture

Title: Node tokens for Text fields appear in token list but don't work in token_replace() » Document that field tokens need proper display modes setup
Component: Code » Documentation
Status: Postponed (maintainer needs more info) » Needs review
FileSize
299 bytes

Thanks for taking a look at this!
Let's see... I didn't set up this site...
- On the Default view mode, field_photo_credit is set to display using the "Default" formatter.
- Ah, there you go! On the Tokens view mode (which I sadly had not noticed), it was set to hidden. Doh!!

So, maybe this should be added to the documentation? There's not much in the README, but I bet this comes up a lot? Here's a quick patch for the README...

jhodgdon’s picture

Also, it's confusing to me that it would show up in the list of available tokens, and then not be generated as an actual token?

Dave Reid’s picture

Category: bug » support

I'm confused why we need to list in in README.txt when this page is already linked from the 'Read documentation' link on the project? I guess I'm open to making this easier to find

We'd have to test in field_token_info() if the field is set to display somehow any of the bundles that it is attached to, which would add great complexity. It is just easier to rely on the site builder to have it configured they way they want it, and always list the token. Also we list tokens solely by entity type, so it's nearly impossible to give you a much more "focused" token list because we can only show you all tokens that apply to nodes, which includes all fields attached to all node types, even though in your case some fields may not apply. It's a hard balance between API limitations, complexity, and usability.

jhodgdon’s picture

Status: Needs review » Fixed

OK. :)

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

added summary