3

How can I apply a conditional fitler to a view (using views 2)?

For example: I have a view which displays the teasers for several different node types using a filter on Node:Type in [Blog entry, Forum topic, User profile]

This works well for showing all nodes of those types, but suppose I only want to show a user's profile if it has a picture as well (and all of the other types).

3

clone a display for the "filtered (must have picture)" view. Add a relationship on the picture field and make it required. This will limit the display of content to that which has a picture field populated.

Now you can use a link in the headers to jump back and forth or provide another means.

2

The method I use for this is to override the page callback and then provide my own custom code for manipulating the view. Here's an example:

  $view = views_get_view('profile_listing');
  // The display name is has been renamed in a a features export. It's more likely to be page_1 or block_1, something along those lines.
  $view->set_display('page_profile_listing'); 

  if (some condition) {
    $date_filter = array(
          'date_filter' => array(
            'operator' => '>=',
            'value' => array(
              'value' => NULL,
              'min' => NULL,
              'max' => NULL,
              'default_date' => 'now -7 days',
              'default_to_date' => '',
            ),
            'group' => '0',
            'exposed' => FALSE,
            'expose' => array(
              'operator' => FALSE,
              'label' => '',
            ),
            'date_fields' => array(
              'node_data_field_feature_updated_date.field_feature_updated_date_value' => 'node_data_field_feature_updated_date.field_feature_updated_date_value',
            ),
            'date_method' => 'OR',
            'granularity' => 'day',
            'form_type' => 'date_select',
            'default_date' => 'now -7 days',
            'default_to_date' => '',
            'year_range' => '-3:+3',
            'id' => 'date_filter',
            'table' => 'node',
            'field' => 'date_filter',
            'override' => array(
              'button' => 'Use default',
            ),
            'relationship' => 'none',
          ),
        );

        $existing_filters = $view->display_handler->get_option('filters');
        $view->display_handler->override_option('filters', array_merge($existing_filters, $date_filter));
  }

  return $view->execute_display('page_profile_listing');

The actual filter I'm adding is nonsensical in this case (it's just copied/pasted from somewhere else I'm doing this). You can get the specific filter you need to add by adding it to the view, using the export function, and finding that specific filter in your export code.

Hope this all makes sense, feel free to ask for more details if you need it.

  • where to put your code in? thank you – enjoylife May 19 '11 at 6:04
1

You can pass the user id as an argument to the view and set a php code validator to check if the user has a picture for exemple.

1

You would want to add other filters to create the new view.

If you want to make the view change based on different conditions you could add arguments that limit the output.

If you want to get really complicated you could write a module that filters through the view results manually and only displays content based on your arguments.

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.