Date fields which have end dates are exposed to the entity token system as structs with three properties: value, value2, and duration. When a date field is configured to have multiple values, then the token type is set to list<struct>.
These are currently not supported, which can lead to either the token being rendered as "Property 0" or to fatal PHP errors which interrupt the page rendering process. See #1618200: Fix date tokens for multiple values (fatal php error) for details on the date example and steps to reproduce.
Underlying problems are (as far as I was able to identify):
- There is no "default value" for structs, so if a token is specified without the suffix ":value" or ":value2", then the token replacement value falls back to the string representation of the respective wrapper. For structs within a list that representation is "Property 0".
- Currently all properties which have a "property info" key (i.e. have sub-properties?) are treated as structs in
_entity_token_map_top_token_type(); this includes lists of properties (list<struct>). This can lead to cases where entity_token_tokens() is called with type=struct and data wrapped into EntityListWrapper, which eventually can lead to a fatal error...
I'll upload a patch which tries to solve these problems in a few minutes...
Comments
Comment #1
bforchhammer commentedAttached patch does the following:
list<struct>. In order for structs to be built properly, the list wrappers need to be constructed with the respective field property info array (--> passed via the token info array).@start-date to @end-date.The patch will probably need more work and testing (I may have broken things); setting to "needs review" to get feedback on the general approach... I'm no expert on the entity api; is this going into the right direction?
Comment #2
matmasr commentedSo will this patch fix the issue related to Date fields with multiple dates ? I can test this patch on a development site to see if it works.
Comment #3
bforchhammer commentedYes; with the patch tokens of the following format should work: [node:field_date:0:value], [node:field_date:0:value2], [node:field_date:1:duration], ...
Comment #4
matmasr commentedBut the issue I am having is not the extraction of the Date "Repeat" tokens. I am interested in getting the following token to work
Do you think this patch will fix that ?
Thanks!
Comment #5
bforchhammer commentedIf I remember correctly, I think I did successfully test
[node:field_date:0:value:short](where "short" is a date format), so theoretically[node:field_date:0:value:custom:Y]should work as well...Comment #6
lhubbert commentedI can confirm that the patch worked for me.
(I literally just did a happy dance around my desk after testing it - thank you!)
Comment #7
bcobin commentedI tried the patch in relation to this issue: http://drupal.org/node/1872468
No joy.
There seems to be active work going on here on what looks to be a thorny issue - any guidance much appreciated... thanks!
Comment #8
mstef commentedWhat about this token: [entity:field:format]? That results in a fatal error with and without this patch.
Here's an example:
I tried using it as [comment:comment-body:plain_text] (and a few other ways..). No luck.. My fault?
Error with patch:
Error without patch: (same as #1440928: Entity tokens for multi-value fields produce a fatal error)
Comment #9
xano#1: struct-lists-1621084-1.patch queued for re-testing.
Comment #10
xano#1944304: Expose payment status items to Entity/Rules depends on this issue.
Comment #11
xanoI am getting field-related exceptions when testing Entity, both with and without the patch. The testbot branch tests are all green, though.
// Edit: I just refreshed my testing installation to 7.x-dev with the testing profile. The tests still cause exceptions.
Comment #12
xanoIf we want this (I'm not sure this is a good thing), we should use a different key to indicate the callback. This is horribly NOT self-descriptive. What about "replacement callback"? We can then use that callback for everything and not just structs, which means that the callback is a feature rather than a workaround.
// Edit I just realized my issue is primarily with Rules data tokens.
Comment #13
jromine commentedThis fixed my issue with date fields using tokens like:
@mstef: there's a patch in #34 of #1440928: Entity tokens for multi-value fields produce a fatal error which might fix the undefined property issue.
Comment #14
chiebert commentedThe patch in #1 didn't work for me, at least when as far as a pathauto (7.x-1.2) pattern rule goes. Date 7.x-2.6, Entity 7.x-1.1 (patched) or 7.x-1.x-dev (patched). I've got a content type with a repeating date (which means its cardinality is unlimited), and an end date. Results:
Comment #15
miguel commentedThe same problem here with File (Field) Path module:
If I try to use
[node:field-datum:0]I get the error that this token is not valid (Dateipfad verwendet folgende ungültige Tokens: [node:field-datum:0]).Comment #16
13rac1 commentedRelated giant issue: #1103032: Document how to use date tokens
With this applied, I am still unable to get start/end dates. Looking into why...
Comment #17
bforchhammer commentedSomething I've noticed just now: You're using field tokens with dashes; there's also the ones with underscores... Not sure whether it really makes a difference, but they are provided by different modules (token vs. entity_token module) so it may well be. From my comments above, I think I only tested my patch above with the underscore version.
Comment #18
13rac1 commentedUnderscore has the same issue:
Comment #19
bforchhammer commentedHm, I tested the patch above with automatic entity label module which does not validate tokens at the moment (see #2123733: Validate tokens). So the token validation function still thinks that the token is invalid (
token_get_invalid_tokens()is the relevant function). From looking at that function maybe we also need to mark anylist<struct>tokens as "dynamic" tokens?Comment #20
lokapujyaI opened #2185745: Support text_formatted token replacement. The patch in this issue seems to partially fix my tokens. Has anyone made any improvements to this patch yet? I might work on it, but don't completely understand the fix yet.
Comment #21
wickwood commentedSo my particular problem was that I had a date field with starting and ending dates with the possibility of repeating.
Upon trying to setup pattern for the URL aliases path, I discovered that the tokens did not work.
However, I was able to apply the patch from #1 to dev version of the Entity module committed on May 25, 2012. (This is actually version 1.0-rc3, SHA ID:c1a949c89aac9bedf11fa8004ac983c2fd7a6ffd)
I then merged that with most current dev version as of today (committed on 4/8/2014 SHA ID: 4d2cc6fb1ecba4409cb8d1b813133366dea2c460). I've tried to make new patch, but I don't have much experience with that and haven't been able to make one that will apply successfully.
Anyway, this did allow me to use tokens to create patterns for the URL aliases path for date fields that end dates, but not repeating dates, with the following token structure for example:
Since I don't really need repeating dates at the moment, this has solved my problem enough to move forward. Hope some else finds this helpful. If I can figure out what is going wrong with my patch creation I will post that later.
Comment #22
rwilson0429 commentedUsing the dev versions of Path Auto, Entity API and Entity Tokens modules, I was able to setup a URL Alias path using tokens for content having a custom repeating date field (field_event_date) configured to have a start and an end date and with unlimited cardinality.
event/[node:field-event-date:0:value:custom:Y]/[node:field-event-date:0:value:custom:m]/[node:title]This created a url path of event/yyyy/mm/title (e.g "event/2015/11/My Content Title") based on the date value in the field_event_date field of the node.
Without adding the cardinality value, "0" in my case, to the format, the tokens didn't work.
Comment #23
dadderley commented@rwilson0429
Thank you very much.