0

I am displaying Flag links via Views. By default the Flag module provides settings to control whether the Flag link appears eg. only appear on own content, only appear on others content etc.

The dilemma is that when displaying the Flag links via Views this no longer has any effect, and the Flag link will always display in the View. I have two Flags; one for owners of content and another for non-owners of the content. Depending on whether the user viewing the content owns the content or not I would like to display one Flag link or the other. This will allow owners for example to "delete" their own content, while non-owners to only hide content which is not theirs. If Views fields respected permissions this would be straight forward, but they don't seam to at least in my experience. How can this be done?

enter image description here

2
  • 2
    Views does respect the field permissions. Looks like you are looking at the view as an admin, try looking at it as a user with proper role.
    – Gokul N K
    Commented Jul 6, 2014 at 13:50
  • It doesn't seem to do this for Flag links displayed via Views though. I tested this logged in as different users and both of the Flag links always show, even though that the Flags have different visibility conditions set in the Flag edit settings.
    – FrontEnd
    Commented Jul 7, 2014 at 11:48

2 Answers 2

1

Take global php field in view and write below logic in.

global $user;
$node=node_load($nid); 
$author=$node->uid;

if($user->uid==$author)
{
   print flag_create_link('flag_machine_name', $nid); // flag for author
}
else
{
  print flag_create_link('flag_machine_name', $nid);//flag for other users.   
}

Hope this will help.

NOTE : replace flag_machine_name with your specific flag's machine name which you want to display.

9
  • I haven't used the Global PHP field before, but your logic does seem sound. When I use your code in the "Value Code" box of the Global PHP field though it doesn't display anything and creates several errors: Notice: Undefined variable: node in __lambda_func() Notice: Trying to get property of non-object in __lambda_func() Am I doing it correctly?
    – FrontEnd
    Commented Jul 7, 2014 at 12:04
  • 1
    $node = node_load($nid); Add this code after global $user;
    – Gokul N K
    Commented Jul 7, 2014 at 12:29
  • 1
    Write the code in output box and not in value box.
    – D-pak
    Commented Jul 8, 2014 at 5:26
  • 1
    you are passing $nid to the node_load() function. From where did u get $nid? You have not assigned any value to it. If you are getting it from url assign proper value ex $nid=arg(i) (i may be any integer). if your url is like base_url/node/3454 i will be 1. Or else you can pass as $node=node_load(arg(i)). Test by passing hard code to the $nid and check.
    – D-pak
    Commented Jul 8, 2014 at 11:33
  • 1
    in case of view you can take nid field and can assign it in php like $nid=row->nid.
    – D-pak
    Commented Jul 8, 2014 at 11:39
0

You don't need any code. I have tested it and views works perfectly with flags permission settings. Just check your flag and view setting once more.

UPDATE :
You have to add relationships with both the flags and you have to add the flag link twice, once for each relationship.

4
  • I'm not sure how you mean this can be done. I have relationships setup for each flag and I have each flag link added as a field. However for me this displays all of my flag links all of the time irrespective of which user is currently logged in and irrespective of whether a flag was set to only show on a owners content or only show on other's content.
    – FrontEnd
    Commented Jul 8, 2014 at 11:04
  • While editing/creating your flag did you choose User may flag only content they own for Delete flag and User may flag only content of others for Hide flag
    – msmani
    Commented Jul 8, 2014 at 11:13
  • Yes, exactly like that
    – FrontEnd
    Commented Jul 8, 2014 at 11:35
  • can you post your view export and the screenshot of the flag's edit page.
    – msmani
    Commented Jul 8, 2014 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.