Using the Dev load page of the Devel module on any node that has flags attached, it became evident to me that there's no direct way to get at a node's flags. It seems that perhaps using the Flags module without the Views module is possible, but quite limited with what you can do regarding a node's presentation of flaged information or any custom php rules or workflows based on a node's flags. The only really visible thing seems to be the Flag this links that get added during page render.
This is kind of a simple request, not sure how useful it will be to others, but I'll offer it up anyway.
Solution:
By adding a few lines to flag_nodeapi() to handle the load operation, anyone can access a nodes flags, and thus quickly and elegantly customize a flagged node's presentation using the node's $node->flags array.
Add the following case just after the end of case 'delete': section of flag_nodeapi():
// Expose user-defined flags attached to a node in $node->flags
// NOTE:
// There's a subtle difference between a node with member "flags" and "flag":
// $node->flags [a node having user-defined flag(s) attached]
// and
// $node->flag [an internal maintenance object used by flag module to administer flags]
case 'load':
if($flags = flag_get_flags('node', $node->type, $user)) {
$node->flags = $flags;
}
break;
The structure of $node->flags will look something like this:
Array
(
[2] => stdClass Object <--- array index (presumably a string '2') represents the flag id ? (not sure why?)
(
[fid] => 2
[content_type] => node
[name] => wishlist
[title] => Wishlist
[flag_short] => Add to Wishlist
[flag_long] =>
[flag_message] => Added
[unflag_short] => Remove from Wishlist
[unflag_long] =>
[unflag_message] => Removed
[roles] => Array
(
[0] => 2
)
[global] => 0
[show_on_page] => 1
[show_on_teaser] => 1
[show_on_form] => 0
[types] => Array
(
[0] => my_custom_wishlistable_node_type <----- $node->type name
)
)
)
I understand that someone might bring up the issue that hooking into _nodeapi 'load' operation will add unnecessary processing (and thus longer load time) but, really, how many flags is any one node going to have and how many node_types on average does a site have?
Comments
Comment #1
mooffie commentedJames, thanks for the time you took to summarize this.
Populating $node->flags in hook_nodeapi is a thought that occurred to all the maintainers of this module, probably. It should be considered. There are more pressing issues to deal with before the '1.0' release. We'll refer to this issue in the future.
===
You're using the "official" version. It's horribly old. Some highlights of the "dev" version:
- You can do the following in node.tpl.php:
- A template-based theming (see 'theme/README.txt'), and It's possible to put a flag-link using code.
Much of the code has been revamped. The API still needs some work. (I'd love to hear your suggestions.) Most of the work done is not yet documented.
Also, there's Rules support comming soon.
Comment #2
quicksketchThis wouldn't be a terrible performance hit, since flags are only loaded once per page load no matter how many times they are used. So even in a situation where displaying a list of nodes, no additional queries would be performed (though we'd still end up doing loops through all available flags every retrieval).
I'm still concerned about the unnecessary loading of data, as jrguitar21 demonstrates, it only takes 2-3 lines of code to get the applicable flags, so it's not clear to me that the benefit outweighs the cost.
Comment #3
mitchell commentedMoving
Comment #4
quicksketchAfter a year of consideration, I still don't think this is worth it. With little continued argument for including the flags in the $node object, I think we can close this.
Comment #5
steveoliver commentedThis method (#1) works great with the 5.x-1.1 version I have running on my site. Thanks for sharing!
-Steve