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
    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 Jul 6 '14 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 Jul 7 '14 at 11:48

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

global $user;

   print flag_create_link('flag_machine_name', $nid); // flag for author
  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.

  • 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 Jul 7 '14 at 12:04
  • 1
    $node = node_load($nid); Add this code after global $user; – Gokul N K Jul 7 '14 at 12:29
  • 1
    Write the code in output box and not in value box. – D-pak Jul 8 '14 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 Jul 8 '14 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 Jul 8 '14 at 11:39

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.

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

  • 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 Jul 8 '14 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 – wolverine Jul 8 '14 at 11:13
  • Yes, exactly like that – FrontEnd Jul 8 '14 at 11:35
  • can you post your view export and the screenshot of the flag's edit page. – wolverine Jul 8 '14 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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