I'm new to Drupal, so I may be missing some "obvious" things

I've installed the flag module, so users can flag their content to make it appear on top of other views. The thing is, when I display the list of content to the user (including both published and unpublished nodes), flag shouldn't be available if the node is unpublished

I tried to alter the view using the hook hook_views_pre_render, but I don't seem to have access to the flag content for removing (I guess my hook is being called after the flag hook, but I'm not certain about that). I tried this:

function mymodule_views_pre_render{
  switch ($view->name) {
    case 'usernodes':
      foreach ($view->result as &$row) {
        //Custom field with publish states, 3 means it is published
        if ($row->field_publish_state[0]['raw']['value'] !== '3') {
          //Unset the two values related to flags I could find

But the flags are still rendered

What I think could work:

  • I could use the hook hook_views_post_render, and edit the second parameter ($output) but it feels wrong to actually override the HTML output (it would become a messy code)
  • I could change it using some Javascript, when the page loads, but it doesn't feel right
  • Using Views PHP and then inserting a Global PHP field (as noted by code.rider below), but users have access to views and they could make a mess with that field

What would be the best way to hide the flag field for certain rows?

  • What is the url of contributed module FlagS? Jul 7, 2017 at 17:14
  • Sorry, didn't know about it, it's actually flag: drupal.org/project/flag
    – Piyin
    Jul 7, 2017 at 17:18
  • @Piyin you might have luck implementing Flag per node. It restricts flag access on a per-node basis. Two disclaimers; 1. This is module of myself. 2. I have never tested it with Views fields. But it might be worth a try :) Jul 13, 2017 at 13:47

1 Answer 1

  • Install the Views PHP module and enable it. This will allow you to embed php into a view cell.
  • Add a new nid/node id field to your View. Make sure to check the “Exclude from display” checkbox.
  • Add a new Content: Published field to your View. Make sure to check the
    “Exclude from display” checkbox.
  • Rearrange your fields so that your hidden nid field and published field at the top of all fields. This is important. It needs to be “available” as an element to the custom php field we are about to add, and if it’s after, the custom php field won’t see it.
  • Add a new Global: PHP field.
  • add the following code to the Output Code section of the field (in php):
    if ($row->status) {
      print flag_create_link("the_machine_name_of_your_flag", $row->nid);
     Use  delimiters to enclose PHP code. 
  • Click the apply button

use flag field in Sort criteria for sorting content mean flagged content on top
this is a tested solution.
hope this solve your problem.

  • Is there another way without using the Global PHP field? Users have access to views and they could make a mess with that field
    – Piyin
    Jul 13, 2017 at 13:36
  • i think no because flag module don't have any setting for display flag link only on published node flag module should have this settings
    – code.rider
    Jul 14, 2017 at 7:12
  • i think you question is now answered alternate of php field is another request so please mark my answer as answered thanks
    – code.rider
    Jul 14, 2017 at 7:30
  • Thanks for the information, but I still don't think this is the best way. I upvoted it because I didn't include that option in my list of things that could work (and it might help others), but I still think Javascript is the best approach, out of all three, for my case (although it doesn't really convince me yet)
    – Piyin
    Jul 14, 2017 at 13:53

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.