Skip to main content
3 of 4
edited title
Piyin
  • 109
  • 4

Hide flag field from certain nodes in view

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

I've installed the flag module [https://www.drupal.org/project/flag], 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
          unset($row->flagging_node_entity_id);
          unset($row->flagging_nodeuidisnotnull);
        }
      }
      break;
  }
}

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

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

Piyin
  • 109
  • 4