I am making a content template, and would like to get the users who flagged a node (non-global flags) when the node is being displayed. How would I go about that? On reading the API page I found how to get the number of people who flagged, but not how to find their uid's (I will be using that to find some information from the user's profiles for display alongside the document).

Comments

mariusooms’s picture

This is what I use (code provided by Moofie) to display the user pictures. You can display anything from the user though.

<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $node = node_load(arg(1));
  $nid = $node->nid;
  $accounts = module_invoke('flag', 'get_content_flags', 'node', $nid);
  if (isset($accounts)) {
    foreach ($accounts as $uid => $data) {
      $account = user_load(array('uid' => $uid));
      print theme('user_picture', $account); //Substitute this with whatever you'd like to display from the user.
    }
  }
}
?>

Hope this helps.

On the subject though, I wish I could make this loop through an ajax check so it would update in real time when you add yourself. Is that even possible?

Regards,

Marius

mooffie’s picture

On the subject though, I wish I could make this loop through an ajax check so it would update in real time when you add yourself. Is that even possible?

Sure it's possible. See Triggering JavaScript actions.

mooffie’s picture

@Ninja,

See the handbook for instructions on how to do this with Views. Then you can embed the view in your node.

@Marius,

Note that your code lists all users who flagged this node, whether they "bookmark"ed it or "spam"ed it. To filter for a specific flag you need to examine $data. Perhaps somebody should explain this in a handbook page. (I remember that you used D5 when you asked your question, so Views wasn't applicable for you because D5's Views can't list users.)

mooffie’s picture

[...] because D5's Views can't list users.

(Well, the handbook actually explains how to produce a list of users in D5. But I agree that coding stuff ourselves is sometimes easier.)

mariusooms’s picture

Sure it's possible. See Triggering JavaScript actions.

Great news...I see the handbook has all kinds of new stuff!

Regards as always,

Marius

mariusooms’s picture

Hi mooffie...I'm having a relatively small problem.

I have a block configured and set to refresh using the code from the handbook. The view is working correctly, however the content halts on the newHtml somewhere. The old content disappears, but is not replaced by the new content. On manual refresh the changes are shown.

Now...messing with view, I noticed if I remove the argument to load "Node ID from URL" the view is refreshing by ajax correctly (removing and adding). The only problem with this is, obviously, that I'm letting the view pull data from all the nodes.

Any idea what I'm doing wrong? I looked over it myself for quite some time now and really can not figure it out.

Regards,

Marius

mooffie’s picture

[...] using the code from the handbook [...] somewhere. The old content disappears, but is not replaced by the new content.

[...]

I noticed if I remove the argument to load "Node ID from URL" the view is refreshing by ajax correctly (removing and adding)

You took the code from the hanbdook. That code asks Drupal for a block. Nothing more. The URL Views sees is "component/block/views/my_great_view", not "node/123", so it can't extract the node ID, because it isn't there. That's the problem.

A very quick way to solve this:

Say the URL you're constructing in that JS code is "component/block/views/my_great_view". Change it to "component/block/views/my_great_view/123", where that number is the node number. Then type "arg(4)" in the PHP code for the argument.

====

There's a different way to solve this. A better way. Views 2.x already has AJAX capabilities. So it shouldn't be hard to add to it a function to refresh a view (from JavaScript). Open a "feature request". If this gets accepted, refreshing a view would entail only one line of JS code.

Maybe Views already has this feature. I don't know. I haven't touched Drupal in months.

(Yet another solution is to enhance the "component" module. Or to look for a better module.)

mariusooms’s picture

First thanks for thinking with me here...especially when not being involved with Drupal for some time (I know how that feels).

Change it to "component/block/views/my_great_view/123"

I did that like this:

$.get(base_url + '?q=component/block/' + module_name + '/' + block_id + '/70', null, function(newHtml) {

The 70 is not dynamic at the moment, it's just to test this current solution.

Finally to make sure, I de-select "Node id from URL" and set it to "PHP Code" and just type arg(4) for the code there? After doing that the block view doesn't return a value anymore.

I also opened a request at the views module in hope that it offers a better existing solution.

Regards,

Marius

ps. Sorry to bug you with this, I wish it just worked.

mooffie’s picture

Some things to try:

  1. You probably need to do "return arg(4);", not "arg(4);". Sorry about that.
  2. You don't really have to touch the JS code at this stage. Type "http://example.com/component/block/views/my_greate_view/70" *directly* into the browser's address bar, and inspect the result. Leave updating the JS for later, after you see the view works with that argument.
  3. Right now you get an empty response, and it's not clear what it means. There's a solution: Type something into the view's "Empty text". E.g. "No nodes were returned". Then, if you see "No nodes were retuened" you'll at least know that the view executed correctly (and found no argument). If you don't see this message, you'll know something else is broken.
mariusooms’s picture

You probably need to do "return arg(4);"

Got it...should have known that myself :)

Type "http://example.com/component/block/views/my_greate_view/70" *directly* into the browser's address bar, and inspect the result.

Okay, the views argument works I see result for both adding and removing a flag.

Type something into the view's "Empty text". E.g. "No nodes were returned".

Yeah...I have it set up like that. The empty text is not showing either.

Regards,

Marius

Maybe this is not the route to go? I also opened a request in views issue queue (http://drupal.org/node/369693) and this is what merlinofchaos said:

The quick answer is yes, and the file that does it normally is ajax_view.js in the js directory. You will need to turn 'Use ajax' on; If you need a long answer detailing the javascript necessary that will take me a little longer, but in theory it should not be too difficult to study that file and figure out the command necessary to trigger a view refresh.

The problem for me is I know squat about javascript. Also, it is a purely aesthetic request and I can live with it not working for blocks, but it would add a professional touch to the sites functionality.

mooffie’s picture

I also opened a request in views issue queue

Yeah, that's definitely the way to go. Views 2.x already has AJAX capabilities so there isn't a reason to re-invent the wheel with that 'component' module.

I won't be able to help here further, because I can't run Drupal on my system. 'ajax_view.js' should ultimately have a 'refresh()' utility method. It's a pity I can't provide a patch. Maybe some developer will see the need and do so.

===

Okay, the views argument works I see result for both adding and removing a flag.

[...]

Yeah...I have it set up like that. The empty text is not showing either.

Mmmm... So when you type in the address bar all works alright, but it's only the JS that doens't work? Strange, because the JS just mimics the former. Perhaps you have a JS syntax error.

mariusooms’s picture

Thanks mooffie,

I will hold on and wait till there is a solution for the ajax_view.js.

Regards,

Marius

ShutterFreak’s picture

Here's some code I used to identify users that flagged a given node as relevant (custom flag "relevance"). In the "node.tpl.php" template file I wrote a code snippet that only displays this information for one specific user role (called 'privileged user'), and I group the users based on an "organization" field that I added in their user profile:

global $user;
if (isset($user->uid)) { 
	if (in_array('privileged user', array_values($user->roles))) {
		$accounts = module_invoke('flag', 'get_content_flags', 'node', $node->nid);
		if (isset($accounts)) {
			$orgs = array();
			$members = 0;
			foreach ($accounts as $uid => $data) {
				if (isset ($data->relevance)) {
					$members++;
					$account = user_load(array('uid' => $uid));
					$orgs[$account->profile_organization][] = $account->name;
				}
			}
			$num_orgs = count($orgs);
			if ($num_orgs > 0) {
				ksort($orgs);
				print "<blockquote>";
				print "<h2>$members member" . ($members=1?'':'s')
					. " from $num_orgs organization" . ($num_orgs=1?'':'s')
					. " found this relevant</h2><ul>";
				foreach ($orgs as $org => $names) {
					sort($names);
					print "<li><b>$org</b>: " . implode(", ", $names) . "</li>";
				}
				print "</ul></blockquote>";
			}
		}
	}
}

I opted for sorting the entries per organization (via ksort()), and within an organization per user name (via sort()). The data are rendered as an unordered list with one bullet per organization.

grigori’s picture

please tell me where is the HANDBOOK ?? i cannot fin it!

sirkitree’s picture

Go the main module page... of any project. Click the link that says "Read documentation" underneath "Resources".

grigori’s picture

i did that, didn't help i still think i there is somethig wrong with this coz i tried on houndreds different way to set up my view to display the users who flag node and still doesn;t show what it suppose to
if there was actually instruction in the handbook how to do it...that different story
must be something wrong with this

grigori’s picture

continue....form previous post

i got
Relationships
Flags: ownit by current user (ownit is just a name, could be bookmark)
Flags: User's flagged content ownit

Arguments
(user flagged content) Flags: Content ID

Fields
User: Name

Filters
(user flagged content) Flags: Flagged True

and this is displaying:

ewahalladin
ewahalladin
ewahalladin
ewahalladin
ewahalladin
ewahalladin
root
root
root
root
root
root
root
root
root
root
root
root
root
root

and should only:
ewahalladin
root

mooffie’s picture

Greg, the handbook page you're looking for is:

How to setup a "Who's bookmarked this" tab

and this is displaying:

ewahalladin
ewahalladin
ewahalladin
ewahalladin
ewahalladin
ewahalladin
root
root
root
root

It seems that your "Flags: Content ID" argument is optional and you didn't provide it. You have to type some node ID, e.g. "25", at the appropriate place in the URL (or, into the "arguments" box of View's "live preview").

mooffie’s picture

if there was actually instruction in the handbook how to do it...

It seems there's a temporary(?) problem on Drupal.org. Normally, under each page are listed its child pages. But this doesn't happen at the moment. That's why you don't see links to further info on the handbook's front-page. That's why I provided a direct link to the Views recipe you're after.

grigori’s picture

firstly - thank you for help

so ok i did like it said in the handbook notes (thanks for that)
but there is this thing i am interesting in:

"If your view work correctly, you can easily turn it into a block. Add a "Block" display. Since blocks don't get arguments from the URL, you'll have to add the "Node ID from URL" default argument. Lastly, enable your block (via "Administer >> Site building >> Blocks")."

especially the part

you'll have to add the "Node ID from URL" default argument

what is this from URL ? when i click on argument Node ID (it actually Node: Nid) how can i setup this?
Action to take if argument is not present:
Display all values (i suppose)

Validator:
?
flaggable node ?
faggable user ?

--------------------------------------------------------------
there is another thing
on the handbook it says that we have 2 options:
1)

   1. Start a user-type view (as opposed to a node-type view).
   2. Add the "User: Name" field.
   3. Bring in the "Flags: User's flagged content" relationship. This step will link each user with all the nodes she has flagged (To be exact, not with her nodes but with her flaggings). By itself, this step is not very useful; so the next step is:
   4. Use the "Flags: Content ID" argument to filter the nodes (to filter the flaggings, to be exact) to the one having the node ID mentioned in the URL. You may set your page URL to, for example, node/%/who and make it a tab.
   5. You may wish to add the "Flags: Flagged time" field to show, besides each user, the date she bookmarked the node. You may then style your view as a table and make it sortable by either user name or flagging time.

2)

   1. Start a node-type view.
   2. Add the "Node: Nid" argument to filter the nodes to the one having the node ID mentioned in the URL. You may set your page URL to, for example, node/%/who and make it a tab.
   3. Bring in the desired "Flags: Node flag" relationship.
   4. Bring in the "Flags: User" relationship.
   5. Add the "User: Name" field. Associate it with the flag relationship.
   6. Here, too, you may wish to add the "Flags: Flagged time" field to show, besides each user, the date she bookmarked the node.

BUT !!
if you consider BLOCK
If your view work correctly, you can easily turn it into a block. Add a "Block" display. Since blocks don't get arguments from the URL, you'll have to add the "Node ID from URL" default argument. Lastly, enable your block (via "Administer >> Site building >> Blocks").

you cannot go for option 1 because in user-type view in Arguments we dont have option Node : Nid
so we go to option 2
we set up view (default) we add
1)relationship:
Flags: ownit by any user
(flag) Flags: User
2) Arguments:
Node: Nid
3) Fields:
(Flag user) User: Name
(flag) Flags: Flagged time

and know we want this in block right?
so we need this argument "Node ID from URL" but We already have it !? or dont we?
is this "Node ID from URL" = Node: Nid

or i should add bock display and add another node: nid

it is confuzing and it is not working !!! please tell me what i am doing wrong

grigori’s picture

this is what i have after following the handbook:
http://www.freeimagehosting.net/uploads/8b94e02ad4.jpg

[not really what i want, is it?]

mooffie’s picture

so ok i did like it said in the handbook notes (thanks for that)

Did it work?

===

is this "Node ID from URL" = Node: Nid

No. You don't need to add any other argument.

You need to configure the *existing* argument.

Action to take if argument is not present:

That's close, but it isn't this setting. Well, maybe it is. There's a setting that lets you pick a value for the argument. There are several possibilities there, among them the "Node ID from URL" one.

The problem is that I'm not running Drupal and therefore I can't instruct you in the exact mouse-clicks to carry out. If you attach a screenshot here of the argument's setting page I might be able to help.

(BTW: Alternatively, there's also a "PHP code" possibility, which let's you type the PHP code to produce the argument value. You could type return arg(1); there and this would be about the same as the "Node ID from URL" solution.)

there is another thing
on the handbook it says that we have 2 options:
1)
1. Start a user-type view
[...]
2)
1. Start a node-type view.
[...]
BUT !!
[...]

That's a very good point. Perhaps that "Node ID from URL" doesn't exist for user-type views. I tend to believe it does exist.

walker2238’s picture

Regarding this snippet...

if (arg(0) == 'node' && is_numeric(arg(1))) {
  $node = node_load(arg(1));
  $nid = $node->nid;
  $accounts = module_invoke('flag', 'get_content_flags', 'node', $nid);
  if (isset($accounts)) {
    foreach ($accounts as $uid => $data) {
      $account = user_load(array('uid' => $uid));
      print theme('user_picture', $account); //Substitute this with whatever you'd like to display from the user.
    }
  }
}

HOw can I limit the output to a given number.. same... last 10 results?

mariusooms’s picture

Moofie's reply to that code :

"I remember that you used D5 when you asked your question, so Views wasn't applicable for you because D5's Views can't list users."

and he's right...you can now do this easily in views. So the code is no longer necessary.

Regards,

Marius

fabrizioprocopio’s picture

I thought to post here instead to open a new issue.
Relating the guide
http://drupal.org/node/326308
I do not know how I go on about the 4th point.
What have I put in the title of argumet?
the example node/%/who
doesn't let me catch what I have to do
I'm not a php coder, not even a views2 champion user :-)

Last answer relate to that guide: always in 4th point, in argument what I choose for validation filed?

thanks by now
for your time
fab

P.S.: My answers they may be OT since it could be a views' stuff too
but being essentially related also to flag module I thought to ask to you: very active on helping

mitchell’s picture

Version: 6.x-1.0-beta6 » 6.x-1.1
Component: Code » Documentation
Status: Active » Fixed
amir simantov’s picture

Following #22

I was messing with it a while, as well. Then got a an answer from yhager - what there is to do is indeed to edit the argument, and keep on like this:

Under "Action to take if argument is not present" choose "Provide default argument"

Then continue as muffie explained:
Under "Default argument type" choose "Node ID from URL"

Amir

Status: Fixed » Closed (fixed)

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

hansrossel’s picture

If you get duplicates like in #17, it means you made a node view, while it should be a user view.