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').

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

japerry’s picture

Issue tags: +commonslove

Adding 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.

joachim’s picture

> 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?

Dave Reid’s picture

If 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. :/

joachim’s picture

Issue tags: +Novice

> 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!

androidi’s picture

Could 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'?

joachim’s picture

Entity types can set their token type to anything, so we should respect that:

    if (!empty($info[$entity_type]['token type'])) {
      // If the entity's token type is already defined, great!
      continue;
    }

    // Fill in default token types for entities.
    switch ($entity_type) {
      case 'taxonomy_term':
      case 'taxonomy_vocabulary':
        // Stupid taxonomy token types...
        $info[$entity_type]['token type'] = str_replace('taxonomy_', '', $entity_type);
        break;
      default:
        // By default the token type is the same as the entity type.
        $info[$entity_type]['token type'] = $entity_type;
        break;
    }
  }
japerry’s picture

Re #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.

joachim’s picture

Status: Active » Needs work

Thanks for tackling this. There's a few problems with your approach though:

+++ b/flag.tokens.inc
@@ -61,6 +61,13 @@ function flag_token_info() {
+    //Issue #1965760: Manually set taxonomy term flag types because its different.

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.

+++ b/flag.tokens.inc
@@ -61,6 +61,13 @@ function flag_token_info() {
+    switch($flag_type) {

Drupal coding standards require a space between the 'switch' and the (.

Though....

$info[$entity_type]['token type']

Surely the above means we can just lift the token type out of entity info?

alexweber’s picture

Just ran into this using Flag 2.1

Liliplanet’s picture

how perfect thank you .. #7 and #8 fixes the error

   // Add tokens for the flag count available at the node/comment/user level.
   foreach (flag_get_types() as $flag_type) {
     $flags = flag_get_flags($flag_type);
    switch ($flag_type) {
      case 'taxonomy_term':
      case 'taxonomy_vocabulary':
        $flag_type = str_replace('taxonomy_', '', $flag_type);
        break;
    }
     foreach ($flags as $flag) {
       $tokens[$flag_type]['flag-' . str_replace('_', '-', $flag->name) . '-count'] = array(
         'name' => t('@flag flag count', array('@flag' => $flag->get_title())),
Liliplanet’s picture

Issue summary: View changes

Fixing pre to code

joachim’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
8.88 KB

I 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.

Status: Needs review » Needs work

The last submitted patch, 11: 1965760.flag_.entity-tokens.patch, failed testing.

The last submitted patch, 11: 1965760.flag_.entity-tokens.patch, failed testing.

joachim’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 11: 1965760.flag_.entity-tokens.patch, failed testing.

The last submitted patch, 11: 1965760.flag_.entity-tokens.patch, failed testing.

joachim’s picture

Status: Needs work » Needs review
FileSize
9.17 KB

Status: Needs review » Needs work

The last submitted patch, 17: 1965760.17.flag_.entity-tokens.patch, failed testing.

The last submitted patch, 17: 1965760.17.flag_.entity-tokens.patch, failed testing.

joachim’s picture

This 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.

joachim’s picture

Status: Needs work » Needs review
FileSize
8.88 KB

Status: Needs review » Needs work

The last submitted patch, 21: 1965760.21.flag_.entity-tokens.patch, failed testing.

jthorson’s picture

Status: Needs work » Needs review
socketwench’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me, tests pass locally.

joachim’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for testing!

Fixed. Will write a change notice.

joachim’s picture

Status: Fixed » Closed (fixed)

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

Devin Carlson’s picture

A backport of #21 for Flag 7.x-2.x if anyone needs it before they're able to upgrade to Flag 3.x.

Devin Carlson’s picture

FileSize
4.47 KB

Fixed a function name issue with #28. Sorry for the noise.