Commit fc9bcff7 authored by joachim's avatar joachim

Issue #1965760 by joachim: Fixed flags using the wrong token type for entity...

Issue #1965760 by joachim: Fixed flags using the wrong token type for entity types whose token type different from the entity type name. Adds soft dependency on Token module for flag-related tokens on other entities.
parent 99971cb7
......@@ -53,6 +53,7 @@ Recommended Modules
-------------------
- Views
- Session API
- Token, which is required for Flag to provide tokens on flagged entities.
Installation
------------
......
......@@ -7,6 +7,8 @@
/**
* Implements of hook_token_info().
*
* The tokens we provide on generic entities require token module.
*/
function flag_token_info() {
$types = array();
......@@ -74,18 +76,29 @@ function flag_token_info() {
'description' => t('The current count total for this flag.'),
);
// 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);
foreach ($flags as $flag) {
$tokens[$flag_type]['flag-' . str_replace('_', '-', $flag->name) . '-count'] = array(
'name' => t('@flag flag count', array('@flag' => $flag->get_title())),
'description' => t('Total flag count for flag @flag', array('@flag' => $flag->get_title())),
);
$tokens[$flag_type]['flag-' . str_replace('_', '-', $flag->name) . '-link'] = array(
'name' => t('@flag flag link', array('@flag' => $flag->get_title())),
'description' => t('Flag/unflag link for @flag', array('@flag' => $flag->get_title())),
);
// Add tokens for the flag count available at the entity level.
// These require token module because we need its helper data and functions
// to deal with token types that are not the same as the entity types they are
// for (in particular, terms and vocabularies).
if (module_exists('token')) {
$entity_info = entity_get_info();
foreach (flag_get_types() as $flag_type) {
// The flag type is the entity type, but this is not necessarily the same
// as the entity's token type.
$token_type = $entity_info[$flag_type]['token type'];
$flags = flag_get_flags($flag_type);
foreach ($flags as $flag) {
$tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-count'] = array(
'name' => t('@flag flag count', array('@flag' => $flag->get_title())),
'description' => t('Total flag count for flag @flag', array('@flag' => $flag->get_title())),
'flag-type' => $flag_type,
);
$tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-link'] = array(
'name' => t('@flag flag link', array('@flag' => $flag->get_title())),
'description' => t('Flag/unflag link for @flag', array('@flag' => $flag->get_title())),
'flag-type' => $flag_type,
);
}
}
}
......@@ -155,18 +168,23 @@ function flag_tokens($type, $tokens, array $data = array(), array $options = arr
}
}
if (isset($data[$type]) && in_array($type, flag_get_types())) {
$flags = flag_get_flags($type);
$object = $data[$type];
foreach ($flags as $flag) {
foreach ($tokens as $name => $original) {
$flag_count_token = 'flag-' . str_replace('_', '-', $flag->name) . '-count';
$flag_link_token = 'flag-' . str_replace('_', '-', $flag->name) . '-link';
if ($name == $flag_count_token) {
$replacements[$original] = $flag->get_count($flag->get_entity_id($object));
}
elseif ($name == $flag_link_token) {
$replacements[$original] = flag_create_link($flag->name, $flag->get_entity_id($object));
// We only provide tokens on entity types if we have token module's helper
// methods available.
if (isset($data[$type]) && module_exists('token')) {
$entity_type = token_get_entity_mapping('token', $type);
if ($entity_type && in_array($entity_type, flag_get_types())) {
$flags = flag_get_flags($entity_type);
$object = $data[$type];
foreach ($flags as $flag) {
foreach ($tokens as $name => $original) {
$flag_count_token = 'flag-' . str_replace('_', '-', $flag->name) . '-count';
$flag_link_token = 'flag-' . str_replace('_', '-', $flag->name) . '-link';
if ($name == $flag_count_token) {
$replacements[$original] = $flag->get_count($flag->get_entity_id($object));
}
elseif ($name == $flag_link_token) {
$replacements[$original] = flag_create_link($flag->name, $flag->get_entity_id($object));
}
}
}
}
......
......@@ -463,6 +463,161 @@ class FlagAccessFormTestCase extends FlagTestCaseBase {
}
/**
* Tokens we provide on generic entities.
*/
class FlagEntityTokensTestCase extends FlagTestCaseBase {
/**
* Implements getInfo().
*/
public static function getInfo() {
return array(
'name' => 'Flag: Entity tokens',
'description' => 'Tokens for flag count on entities.',
'group' => 'Flag',
);
}
/**
* Implements setUp().
*/
function setUp() {
// Our entity tokens require token module.
parent::setUp('flag', 'token');
}
/**
* Test tokens on nodes.
*/
function testNodeFlagToken() {
// Create a flag on article nodes.
$flag_data = array(
'entity_type' => 'node',
'name' => 'node_flag',
'title' => 'Node Flag',
'global' => 0,
'types' => array(
0 => 'article',
),
'flag_short' => 'Flag this item',
'flag_long' => '',
'flag_message' => '',
'unflag_short' => 'Unflag this item',
'unflag_long' => '',
'unflag_message' => '',
'unflag_denied_text' => 'You may not unflag this item',
'link_type' => 'normal',
'weight' => 0,
// Show the flag on the form.
'show_on_form' => 1,
'access_author' => '',
'show_contextual_link' => 0,
'show_in_links' => array(
'full' => 1,
'teaser' => 1,
),
'i18n' => 0,
'api_version' => 3,
);
$flag = $this->createFlag($flag_data);
// Create a node to flag.
$node = (object) array(
'type' => 'article',
'title' => $this->randomName(),
);
node_save($node);
// Flag it by several users.
$flag_user_1 = $this->drupalCreateUser(array('flag node_flag',));
// Flag the node as the user.
$flag = flag_get_flag('node_flag');
$flag->flag('flag', $node->nid, $flag_user_1);
$flag_user_2 = $this->drupalCreateUser(array('flag node_flag',));
// Flag the node as the user.
$flag->flag('flag', $node->nid, $flag_user_2);
$text = '[node:flag-node-flag-count]';
$replaced_text = token_replace($text, array('node' => $node));
$this->assertEqual($replaced_text, 2, "The flag count token for the node is correct.");
}
/**
* Test tokens on taxonomy terms.
*
* These are worthy of a separate test, as the token type is a special case.
*/
function testTaxonomyTermFlagToken() {
// Create a flag on tag terms.
$flag_data = array(
'entity_type' => 'taxonomy_term',
'name' => 'term_flag',
'title' => 'Term Flag',
'global' => 0,
'types' => array(
0 => 'tags',
),
'flag_short' => 'Flag this item',
'flag_long' => '',
'flag_message' => '',
'unflag_short' => 'Unflag this item',
'unflag_long' => '',
'unflag_message' => '',
'unflag_denied_text' => 'You may not unflag this item',
'link_type' => 'normal',
'weight' => 0,
// Show the flag on the form.
'show_on_form' => 1,
'access_author' => '',
'show_contextual_link' => 0,
'show_in_links' => array(
'full' => 1,
'teaser' => 1,
),
'i18n' => 0,
'api_version' => 3,
);
$flag = $this->createFlag($flag_data);
$vocabulary = taxonomy_vocabulary_load(1);
// Create a term to flag.
$term = (object) array(
'name' => $this->randomName(),
'vid' => 1,
);
taxonomy_term_save($term);
// Flag it by several users.
$flag_user_1 = $this->drupalCreateUser(array('flag term_flag',));
// Flag the term as the user.
$flag = flag_get_flag('term_flag');
$flag->flag('flag', $term->tid, $flag_user_1);
$flag_user_2 = $this->drupalCreateUser(array('flag term_flag',));
// Flag the term as the user.
$flag = flag_get_flag('term_flag');
$flag->flag('flag', $term->tid, $flag_user_2);
$text = '[term:flag-term-flag-count]';
$replaced_text = token_replace($text, array('term' => $term));
debug($replaced_text);
$this->assertEqual($replaced_text, 2, "The flag count token for the term is correct.");
}
}
/**
* Access to flags using the basic flag link.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment