0

I have a custom flag defined only on certain content types and I want to make a convenient list of all nodes that the flag is being displayed on, that is, nodes of all the content types that the flag is defined on.

1

If these content types are never going to change, the quick and dirty would be to just create a View filtering on those content types.

If this is not the case, then you are going to have to get down into some coding as I am not aware of any built in Views/Flags integration on this level. Sure, you can list all flagged or not flagged content, but I don't see a way to list flaggable content.

So, to get the node types, you should be able to do something like this:

$flag=flag_get_flag('NAME_OF_THE_FLAG');

and now

$types=$flag->types;

should be an array of node types this flag can be applied to. So now you need to tell Views about this.

So, using the quick and dirty View created above, you could then alter it up with something along these lines:

function YOURMODULE_views_pre_view(&$view, &$display_id, &$args) {

  if ($view->name=='NAME_OF_THE_VIEW') {

    $flag=flag_get_flag('NAME_OF_THE_FLAG');

    $filters = $view->display_handler->get_option('filters');
    // you may want to dpm() or drupal_set_message() or watchdog() or otherwise look at $filter to see what you have to deal with on your system
    $filters['type']['value'] = $flag->types;

    $overrides = array();
    $overrides['filters'] = $filters;

    foreach ($overrides as $option => $definition) {
      $view->display_handler->override_option($option, $definition);
    }

  }

}

which on my box here, changes the types filtered to those in the flag.

Your $flag and $filters may/will most probably vary, so check to see what you have to work with as these are the broad strokes.

Lots of information about flags here: Flag 3.x PHP API

Information about Views hooks here: Views Hooks

| improve this answer | |
  • Thanks for the detail! I'll try the quick & dirty method first, though. – Turion Dec 27 '13 at 0:17
0

Are you using a custom field or the Flag module?

In both cases, you should be able to create a view and then add filter either on your custom field or on your flag.

| improve this answer | |
  • I'm using the flag module. How do I filter on a flag? – Turion Dec 26 '13 at 17:01
  • I think what you're looking for is this tutorial: drupal.org/node/310165 You actually can't use filter with flags, you need to use Relationships. – Matt Dec 27 '13 at 10:03

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.