Closed (fixed)
Project:
Field collection
Version:
7.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
16 Dec 2011 at 07:46 UTC
Updated:
24 Nov 2015 at 11:56 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
bt82 commented+1
i am having exactly the same problem. any solution would be appreciated.
Comment #2
Anonymous (not verified) commentedWould love to have node tokens work in field collections as well.
Comment #3
mnlund commentedSecond this! The Host entity token doesn't work as expected in Rules. None of the bundle tokens are showing.
Comment #4
areynolds commentedIf you're looking to access field collections through Rules, you can use the data selector in the manner described in this issue.
That said, I agree that some better token integration would be a big step in making Field Collections easier to use.
Comment #5
nicholas.alipaz commentedKind of odd this is not already available seeing how a token module was created. Hope someone can look into this. I may at some point, but not sure if I can do it for my current project due to deadline.
Comment #6
nicholas.alipaz commentedI guess the token module might be part of the entity api, hadn't realized when I posted initially. However, this still seems to be of use and I do wonder if it should be in the queue for Entity API instead.
Anyhow, I have little time to deal with a proper patch so I threw together some code that gets the job done for the one field I need this for. This is likely the completely wrong way of performing the replacement as I believe I should be using the token chaining bit. But again, time is of the essence and I had no time to attempt wrapping my head around chaining. Do note the hardcoded field and table names, this is just in a separate module so that I can get what I need until a proper solution is found:
Comment #7
johazielFirst enabled Entity_token
Then created a module field_collection_token inspired of #6 like this
Now, you will see the Field collection tokens in Replacement patterns of FileField paths
with [field_collection_item:parent-node]
Comment #8
RicardoJBarrios commentedI've tried #7, but a dpm of $host_entity give me an empty result and doesn't work. I've been looking at the API and googling a lot and I've found that in some scenarios hostEntity() doesn't work. I'm using it in File (Field) Paths.
Someone have it working?
Drupal 7.17
Entity API 7.x-1.x-dev (2012-Oct-24)
File (Field) Paths 7.x-1.0-beta3
Field collection 7.x-1.0-beta4
Comment #9
tommychrisSame as #8, its notworking :(
field_collection_token_token() not run, only field_collection_token_token_info().
Here is the .info file for module:
Comment #10
jhoffmcd commentedI am also looking for a fix to this. Hopefully we can all resolve this. I will test as well.
I'm also using File (Field) Paths
Comment #11
neRok commented#7 works for me. You get the nid as described (which is all I wanted), but now that you have the nid you should be able to load all the nodes tokens.
Pre-made module of #7 is attached.
Comment #12
yannickooHere is a more coding standard version of #11. I think it would be better to have the whole node as token and not just the node id.
Comment #13
yannickooForget the extra module, let's put this into Field collection. The problem with this patch is that it does not work for other host entity types than node. Means that this won't work if your field collection is in a user, comment, taxonomy term ... entity.
I have no idea how to fix that because you have to specifiy the type which has to be sth. like 'node'. There is no 'entity' type.
Comment #14
yannickooForgot the patch...
Comment #15
yannickooComment #16
Karthick_s commentedI use this along fileField path .The patch works fine on a saved node. but it does not fetch host entity details on node creation itself. I use embedded widget.
Comment #17
yannickooComment #18
jhoffmcd commentedJust to confirm before I try any of this, you say it does not gather Entity replacement patterns, only Nodes?
Comment #19
katannshaw commented#14 worked great for me. Thanks for your work on this patch.
Comment #20
petrovnn commentedplease include patch #14 in next release, i have absolutely same problem
Comment #21
designerbrent commentedThis looks like it will be helpful, though I can't get it to work. Shouldn't there be a line that goes into the module file to make it load the .token.inc file?
Comment #22
yannickooNo that is not necessary. Did you try clearing the cache?
Comment #23
designerbrent commentedNot sure why it wasn't working yesterday for me but at least the tokens show up for me now. I am having a problem with trying to get the NID to actually go into the first filename.
Comment #24
markj commentedI applied the patch from #14 and the replacement tokens are showing up for me, but when I try to use one of the tokens, I get the following error:
Warning: Illegal offset type in isset or empty in token_tokens() (line 900 of /mnt/u1/var/www/drupal/sfutheses/sites/all/modules/token/token.tokens.inc).It doesn't matter if the token is for a system-generated value, like [field_collection_item:host:nid], or for a content-type-specific field.
Using Field Collection 7.x-1.0-beta5, File (Field) Paths 7.x-1.0-beta4, Token 7.x-1.5, Entity Tokens 7.x-1.1, Drupal core 7.22.
Comment #25
jastylr commentedI'm getting the same error message as well:
Warning: Illegal offset type in isset or empty in token_tokens() (line 900 of /Applications/MAMP/htdocs/cshof-drupal/sites/all/modules/token/token.tokens.inc).I was trying to set the path for an uploaded image in a field collection using File Field Paths and a value from the host content type. I was able to select the appropriate token but when I created the actual content and saved it, I got the error and the image path did not include the value I was trying to set.
Comment #26
jastylr commentedAs an update:
What I have is a content type representing a group of individuals for a particular year. The year is a Date field in the host content type and I have a field collection that includes fields for an individual's name and their image. I have setup the File Field Path for the uploaded image to grab the Year value from the host node. The field collection itself is setup to allow for multiple instances.
I created the content, specifying a year and uploading my first image as part of the field collection. When I save the node, I get the error above in my previous post and the file path for the image does NOT include the year as intended.
I found that by going back and editing the node to remove the image and re-adding it, the error message no longer appears and the image file ends up in the correct path which includes the year value from the host entity. This happens again if I edit the node and add another field collection instance to upload another image. I can then again, go back and edit the node to remove and re-add the image and everything is fine again.
Not sure why this is happening but seems to have something to do with adding versus editing the content?
Comment #27
daniel wentsch commentedThanks for your Patch #14 @yannickoo, but it seems I can't get it to work:
using
- field_collection 7.x-1.0-beta5
- filefield_paths 7.x-1.0-beta3+4-dev
Your patch was successfully applied, field_collection.tokens.inc exists, fluched caches multiple times – but no luck, I don't get any host token replacement patterns shown on the image field inside my field collection (Screenshot attached).
Any ideas? Are you maybe using the dev build of field_collection?
Thanks & regards,
Daniel
UPDATE:
Typically: as soon as I posted about my issue I found the solution. I accidentially used the wrong token browser (below the field description) instead of looking at the replacement patterns inside filefield paths' fieldgroup.
The module now works as desired - perfect, thanks a lot.
Comment #28
daniel wentsch commentedConfirming issues #24/#25.
As jastylr said, this error only occurs upon adding files to new field collection instances. Upon editing an already existing field collection the tokens get properly inserted.
Could anyone for who #14 is working on new field collection instances please post their versions of filefield_paths, entity, token and field_collection?
I'm currently using filefield_path-7.x-1.0-beta4, entity-7.x-1.1, token-7.x-1.5 and field_collection-7.x-1.0-beta5 and wonder if upgrading to dev might help.
Comment #29
maximpodorov commented#14 is fixed to support token info information provided by host entity.
Comment #30
yannickooI removed a single line.
Comment #31
daniel wentsch commentedThanks yannickoo, but the problem remains after successfully patching and clearing the caches.
After adding a new instance of a field collection and saving an (existing) node I still get the following error and the replacement pattern (node id of the host entity) stays empty :(
Warning: Illegal offset type in isset or empty in token_tokens() (Zeile 900 von /foobar/sites/all/modules/contrib/token/token.tokens.inc).Comment #32
cecrs commentedThe patch in #30 works beautifully on already existing field collection items, i.e., if I am editing an existing field collection entry on a node. If I am creating a new field_collection instance, that token fails with the Warning: Illegal offset type in isset or empty in token_tokens(). error.
I think that the basic issue is that the token code is being run before the node is saved, it may be similar to this issue for the domain module: https://drupal.org/node/1336698.
Comment #33
nicholas.alipaz commentedMy guess is
$data[$type]->item_idis not yet available during the insert vs the edit op, but I could be wrong.Comment #34
TheChemic commentedI'm also looking for a solution to this. I need to get the parent node's id on new node creation so that I can keep my image files organized into nid folders.
Comment #35
c3rberus commentedI too am looking for this, would like to use a token value from the host entry.. any change for author to look at the patches submitted and commit something in --dev? :)
Comment #36
bpsommerville commentedChanging the field_collection_tokens method in patch #30 as follows seems to fix the error in #31 for me
Comment #37
markdatter commentedI'm using the code from #36 and I am not getting any errors when creating a new host node. Unfortunately, the token replacement is not working properly when I try to use the nid of the host entity during host node creation. I inserted some debugging lines into the code and I noticed that the nid of $item->hostEntity() is "NULL" when the value of $host_entity is being assigned.
I also noticed that the "else" block is not being executed during new host node creation, nor is it being executed when editing an existing host node. Under what condition would that code be executed?
Comment #38
nicholas.alipaz commentedThis thread is going to go on forever if people don't understand what I wrote earlier:
During the insert op of any entity, the identifier is not yet assigned. The only solution that I can see is to do a query for the auto-increment's next available value or perhaps save the entity twice so you can grab the id.
Comment #39
jody lynnI added a hook_field_attach_insert to resave the field collections right after the host entity's field collection field is created.
This works so long as your tokens are set to change on an update. For example, I'm using filefield_paths and had to set the field settings to 'active updating'.
Comment #41
daniel wentsch commentedPatch #39 didn't apply, I changed paths of diff file to make it apply.
PS: Seems to work now, thanks a lot.
Comment #42
daniel wentsch commentedComment #43
stevendeleus commentedIs it possible to support tokens for any host entity, not just nodes?
Comment #44
daniel wentsch commentedUsing this setup on another site I found another issue with patch #39:
It doens't work when re-saving an existing node with revisioning enabled.
As soon as Created new revision is checked the following good old error comes back and the filefield path is reset to none:
Warning: Illegal offset type in isset or empty in token_tokens() (Zeile 900 von /foobar/sites/all/modules/contrib/token/token.tokens.inc).Comment #45
amogiz commentedHi, i applied with success the patch, but i can't get the [node:title] token from the list.
Dis I miss something ?
Comment #46
amogiz commentedFinally I foud it : the tokens were in Field Collection host entity part.
Thanx for this great patch !!!!!!
Comment #47
markdatter commentedI'm using #41 and everything seems to be working when I upload field collection images during new host entity creation. This works for me without filefield_paths set to 'active updating'. Am I missing something, or have others made this work without that setting?
I don't have revisioning enabled, so I can't speak to the concern raised in #44.
Comment #48
dlomis commented41: field_collection-1372652-39.patch queued for re-testing.
Comment #49
jody lynnI had the same issue as in #44. A node update with revisions on breaks the token.
Here's an updated patch to address that.
Comment #50
jody lynnComment #51
ndobromirov commentedHello guys,
I had similar problem to this one. It was to use tokens from the root node, containing a field collection within field collection.
For example: node -> fild_collection -> field_collection -> field.
The field needs to get tokens provided by the node.
Based on the solution in #50 with some small changes, here is how I made it work:
Add new token:
This helper method:
Token info implementation change:
Probably this not the prefect thing as there are some code repetitions, but let me hear your opinions and I will make a patch for it.
Comment #52
marc.groth commented#50 worked perfectly for me. Thanks Jody Lynn.
Using the following setup:
Drupal 7.26
Entity API 7.x-1.3
File (Field) Paths 7.x-1.0-beta4
Field collection 7.x-1.0-beta5
Comment #53
vincer commentedAfter applying patch in #50, in admin/reports/status I get the following alert:
The following token types are not defined but have tokens ... field_collection_item
When I replace the patched dev version with field_collection-7.x-1.0-beta7 the alert is gone.
UPDATE
I reattempted this and it works now.
Comment #54
czigor commentedI have the same issue. I do have a [field_collection_item:host_entity] token though, but it does not work.
Problem/Motivation
Root of problem
The problem is that when FieldCollectionItemEntity::fetchHostDetails() is called from rules_unwrap_data() the revision_id has not been written into the database yet. Thus the EFQ in FieldCollectionItemEntity::fetchHostDetails() has no result.
Proposed resolution
The patch in #50 is almost good, except that no new tokens are needed.
Comment #55
czigor commentedTypo.
Comment #56
czigor commentedThis patch is not good. It makes entity update hooks run twice, first with correct $entity->original property then with $entity->original being identical to the updated field collection.
Somehow the field collection update workflow should be changed or tweaked.
Comment #57
jody lynnczigor: I agree. It's just a dirty hack for those desperate for the feature.
Comment #58
czigor commentedThe following patch is OK assuming the host entity of a field collection does not change. A hint supporting the validity of this assumption is in the DOxygen of FieldCollectionItemEntity::setHostEntity(): "Sets the host entity. Only possible during creation of a item."
Comment #59
jmuzz commentedYou are correct, the host entity for a field collection item should not change.
I have verified that field_collection_item:host_entity does appear in the data selectors in Rules, but I don't think it's the same thing as a token. Try this:
- Create a view on field_collection_items .
- Add to the Header a "Global: Text area"
- In the settings for the text area check "Use replacement tokens from the first row"
- [item_id] is the only one available.
You can also see this in rules if you go to step 4 of your instructions in #54 and open up the replacement patterns for the message.
I think that #50 should be reviewed here and #58 should be put into a separate issue.
Comment #60
whop commentedHello, is there any update ?
I also need to use host entity token, patch #50 is enabling that.
But it saves correctly only when editing content, thus on second attempt. Not at creation time.
#58 doesn't add host entity.
Using beta 7
Thanks for help or suggestions !
Comment #61
jmuzz commented#58 allows the host to be picked in the data selector in rules but does -not- add a token for it.
#50 is not good as explained in #56 and #57.
Comment #62
veleiro commentedSo I can use rules to get the host entity id? I'm looking to pass it as a views argument.
Comment #63
veleiro commentedI ended up using the following custom token for my situation:
Comment #64
kaareHow should the format be? I've started implementing
field_collection.tokens.inc, but have reached an architectural barrier. The Field collection entity contains some basic information about the host entity that can be provided without loading the host entity and fetch the values from there:FieldCollectionItemEntity::hostEntityType()FieldCollectionItemEntity::hostEntityBundle()FieldCollectionItemEntity::hostEntityId()These can either be provided directly to the
$type == 'field_collection_item'tokens like this:[field_collection_item:host-type][field_collection_item:host-bundle][field_collection_item:host-id]Easy peasy. The major architectural question is how to provide the host entity, as this can be any kind of fieldable, tokenable entity. The token browser knows nothing of this. The idea I've had so far is to provide separate token for each available entity type and follow Organic Group's example in naming. This will leave it up to the site builder to pay attention to what token to use.
[field_collection_item:host--node][field_collection_item:host--node:nid][field_collection_item:host--node:title][field_collection_item:host--node:content-type][field_collection_item:host--user][field_collection_item:host--user:uid][field_collection_item:host--user:mail][field_collection_item:host--field_collection_item][field_collection_item:host--field_collection_item:host--node:nid][field_collection_item:host--field_collection_item:host--user:name]Otherwise, if the host token should be entity type independent, we have to provide all available properties and fields that all entities can have, which creates chaos.
As I've written this I've come to the the conclusion that this is the cleanest way to do it. Any opinions?
Comment #65
kaareOooor.. one could create a new token type
'host'and provide all attributes and nested values there:[field_collection_item:host:type][field_collection_item:host:bundle][field_collection_item:host:id][field_collection_item:host:node:nid][field_collection_item:host:node:content-type:machine-name][field_collection_item:host:node:title][field_collection_item:host:user:uid][field_collection_item:host:user:mail][field_collection_item:host:field_collection_item:type][field_collection_item:host:field_collection_item:host:node:title]Comment #66
kaareAnd here is the implementation as suggested in #65. It works with File (Field) Paths.
The token browser becomes really heavy with this if you have a lot of fieldable entity types available, so the use of Token tweaks module is recommended.
Comment #67
kaareComment #68
kaare*facepalm* Sorry, I've completely ignored major part of this issue, only looked at the first and last comments and patches, which doesn't include the
field_collection.token.incpart. Having read everything my patch also struggles with the case of saving nodes with field collections and revisions enabled. I'll have a closer look at the current and possible other solutions later this week.In any case, I think having a new token type (
'host') and chained entity type tokens after that solves the problem of not knowing what kind of entity type the host is, at least in the browser. This is how it behaves in the token browser:This also lets you select Field collection items as host entities, allowing for complex nested setup.
Comment #69
kaareThis patch fixes tokens during revisions.
There are other concerns that are out of the scope of this issue:
To keep this issue simple let's keep focus: Only provide core token support for the host entity of field collections.
Comment #70
kaareComment #71
gge commentedCan you please build a dev?
Comment #72
kaare@gge: You mean build a test site?
Comment #73
kaareI realize the list of available host entity types can be made smarter if one look at what field collections exists and what they are attached to. This would definitely clean up some mess in the token browser.
Comment #74
kaareLike this. The entity browser becomes much more sane without the noise of entity types not instantiated with field collection fields. This version uses
field_info_field_map()to pick items fromentity_get_info()Comment #75
gge commentedI wanted to use File (Field) Paths for an image field in a field collection and I needed to use a token that belongs to the host node. I was wodering if you are going to push your patch to the dev branch so other people could test it.
Thanks.
Comment #76
nicholas.alipaz commentedgge, a patch is typically only pushed into a branch once it has been reviewed and tested (successfully) by the community. Plus I don't believe kaare to be a mainteinter on the field collection module, nor am I.
kaare, nice work on the patch, I have yet to test but will try to soon.
Comment #77
kaare@gge: Yeh, I'm no maintainer, just in need of this exact feature for my current project. Have a look at drush makefiles to automatically setup a set of modules with selected patches, if you haven't already.
@nicholas.alipaz: Thx! A review of it would help tremendous for the maintainers to get this in.
Comment #78
MatthewHager commentedI just tested this with filefield_paths and it worked perfectly. Thanks for your patch!
Comment #79
jmuzz commentedAll the strings should be single quoted unless they contain variables or single quotes.
All sentences should end with a full stop.
@kaare, did you have any plans to add tests for this? I saw you set the tag for it. If nobody is going to do that soon it can be made a separate issue.
Comment #80
kaareThanks for the review. Points addressed and sprinkled some html as cosmetic on some of the text strings.
@jmuzz: No tests are planned. The tag was in hope that some more capable in writing tests than me would find time for it. It probably should be a follow up issue.
Comment #81
kaareComment #83
jmuzz commentedThanks kaare.
Followup issues still need to be created (or found if they exist) for:
1 - Rules tokens
2 - Views tokens
3 - Test coverage
Should link to this issue and post relevant patches from it (#58 for example).
Comment #84
jmuzz commentedComment #85
kaareFollow-up issues are unrelated to this issue's status. It still needs review, or if you are happy with this please feel free to RTBC it :-)
Comment #86
jmuzz commentedI committed it. See #82. I just don't think the issue should be closed until the followups get made.
Comment #87
kaareHah, you did! Totally missed that. Thx a bunch! :-)
Comment #88
kaareCome to think about views' replacements tokens. This comes out of the box with views using relations? You have to set up a relation in order to access other entities. So this probably doesn't need a follow-up?
Comment #89
zmove commentedTested it and it works partially.
I encountered a problem with referenced fields on the host entity.
The host entity of the field collection is a node, that have a commerce product reference (it's a node display for people who knows drupal commerce).
With the patch, I can access to the node field-product token by using this
And normally, I should be able to access the product SKU by doing that :
This is an available token when I work on the imagefield of my node, but when I work on my field collection. Even with that patch that allow to reference host entity (and all it's children), I got an error :
It seems that the commerce products token are not "parsed" to provide all available token when you are on a field collection.
Any ideas to fix that ?
Comment #90
kaareThis piece of code will leave it to the child tokens to replace the nested values. The entity (node) is passed and then it's up to the child token handler to perform the substitutions. So I would look up the token implementation that handles you
skucode and start debugging from there, then nest myself back up tofield_collection_tokens().Comment #91
osopolarI also run into problems where i need the token
[field_collection_item:host:node:url:path]for the field collection pathauto alias. I didn't get the node alias as expected but the path node/nid. The problem was, that LANGUAGE_NONE was passed instead of the hosts language code.I attached a patch where the host entity's language will be passed as option to token_generate(). I am not entirely sure if that is the correct way to fix it. Or maybe it get fixed somehow with the issue #1344672: Field Collection: Field translation (entity_translation) support.. At least it works for my purposes.
Comment #92
kaare@zmove, @osopolar: Please create a new issues for any new problems. This issue is committed and fixed.
Comment #93
osopolar@zmove, did you find the cause or a solution for the problem with the sku? Would be nice if you test my patch, maybe it fixes your issue too.
As suggested I opened a new follow up issue, please post your answer there: #2567203: Improve token support for host entity.
Comment #94
zmove commented@osopolar sorry, I probably used another method to do what I want. I say probably because in fact, I even don't remember the project that needed this feature. I just switched to something else.
Comment #96
jay.lee.bio commentedIf anyone can solve the same problem for Paragraphs, I'd really appreciate it.