Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
See flag_token_info():
77 // Add tokens for the flag count available at the node/comment/user level.
78 foreach (flag_get_types() as $flag_type) {
79 $flags = flag_get_flags($flag_type);
80 foreach ($flags as $flag) {
81 $tokens[$flag_type]['flag-' . str_replace('_', '-', $flag->name) . '-count'] = array(
82 'name' => t('@flag flag count', array('@flag' => $flag->get_title())),
83 'description' => t('Total flag count for flag @flag', array('@flag' => $flag->get_title())),
84 );
85 $tokens[$flag_type]['flag-' . str_replace('_', '-', $flag->name) . '-link'] = array(
86 'name' => t('@flag flag link', array('@flag' => $flag->get_title())),
87 'description' => t('Flag/unflag link for @flag', array('@flag' => $flag->get_title())),
88 );
89 }
90 }
This causes problems for entities that have an entity type which does not match it's token type (e.g. 'taxonomy_term' vs 'term').
Comment | File | Size | Author |
---|---|---|---|
#29 | 1965760.29.flag_.entity-tokens.patch | 4.47 KB | Devin Carlson |
#21 | 1965760.21.flag_.entity-tokens.patch | 8.88 KB | joachim |
Comments
Comment #1
japerryAdding commonslove tag. It'd be good to have this fixed properly, since we're just covering up the issue by adding a type for the entity.
Comment #2
joachim CreditAttribution: joachim commented> This causes problems for entities that have an entity type which does not match it's token type (e.g. 'taxonomy_term' vs 'term').
Where is the token type for an entity type defined?
Comment #3
Dave ReidIf the token module is installed, it's defined in the 'token type' key/value in the entity_get_info() altered in via token_entity_info_alter(). For most entity types it should be assumed the entity type is the token type, but core is not consistent with this. :/
Comment #4
joachim CreditAttribution: joachim commented> but core is not consistent with this. :/
Hehe, I've heard that before :)
Okay, I think that's enough info that we can tag this as 'novice' :) Thanks!
Comment #5
androidi CreditAttribution: androidi commentedCould you not just add the following somewhere between lines 80-81 in flag_token_info():
if ($flag_type == 'taxonomy_term') { $flag_type = 'term'; }
since it's extremely unlikely that core in D7 would be changed from using the 'term' token to 'taxonomy_term'?
Comment #6
joachim CreditAttribution: joachim commentedEntity types can set their token type to anything, so we should respect that:
Comment #7
japerryRe #6: you're talking about this right?
http://drupalcontrib.org/api/drupal/contributions!token!token.module/fun...
I've just gone ahead and put in a patch that fixes this for taxonomy terms similar to the token module below. While this fixes this specific issue, I feel that this isn't totally the correct spot for it. But without debugging flag more, I'm not sure where else it'd go.
Comment #8
joachim CreditAttribution: joachim commentedThanks for tackling this. There's a few problems with your approach though:
Code shouldn't refer to issues unless it's an ongoing TODO, or a problem that can't be summarized and really requires the background reading that's at the issue.
Drupal coding standards require a space between the 'switch' and the (.
Though....
Surely the above means we can just lift the token type out of entity info?
Comment #9
alexweber CreditAttribution: alexweber commentedJust ran into this using Flag 2.1
Comment #10
Liliplanet CreditAttribution: Liliplanet commentedhow perfect thank you .. #7 and #8 fixes the error
Comment #10.0
Liliplanet CreditAttribution: Liliplanet commentedFixing pre to code
Comment #11
joachim CreditAttribution: joachim commentedI had a tinker with this yesterday.
While it's easy to get the entity info to get the token type from the entity type when we're defining token info, the same is not true when we're performing token replacements. We have a token type which might or might not be defined in entity info, and the only way to know is to check the whole of entity info.
Fortunately, token module has helper functions for this. It also does the job of defining 'token type' in the info for all entities, so we can rely on its presence.
Therefore, I think we should make the tokens that Flag provides on general entities depend on the presence of token module.
Comment #14
joachim CreditAttribution: joachim commented11: 1965760.flag_.entity-tokens.patch queued for re-testing.
Comment #17
joachim CreditAttribution: joachim commentedComment #20
joachim CreditAttribution: joachim commentedThis is failing due to #2181107: test fails when adding another contrib module to enable in setUp().
I'm going to commit the test_dependenies separately and make a new patch for this issue.
Comment #21
joachim CreditAttribution: joachim commentedComment #23
jthorson CreditAttribution: jthorson commented21: 1965760.21.flag_.entity-tokens.patch queued for re-testing.
Comment #24
socketwench CreditAttribution: socketwench commentedLooks good to me, tests pass locally.
Comment #25
joachim CreditAttribution: joachim commentedThanks for testing!
Fixed. Will write a change notice.
Comment #26
joachim CreditAttribution: joachim commentedhttps://drupal.org/node/2192019
Comment #28
Devin Carlson CreditAttribution: Devin Carlson commentedA backport of #21 for Flag 7.x-2.x if anyone needs it before they're able to upgrade to Flag 3.x.
Comment #29
Devin Carlson CreditAttribution: Devin Carlson commentedFixed a function name issue with #28. Sorry for the noise.