2

I have a view and I programmatically created a filter for this view. (I pasted the code below).

This filter works fine but I had other Exposed Filter created inside the view (created by clicking add filter in the view).

Now the filter added programmatically is working fine but the other one is not shown at all.

If I disable the filter added programmatically then the other one is shown correctly. So the problem is in the code I guess.

I need both filter working together.

here is the code which adds the filter:

/**
 * Implements hook_views_pre_view().
 */
function vouchers_views_pre_view(&$view, &$display_id, &$args) {

   if ($view->name == 'vouchers') {
    $info = array(
      'table' => 'field_data_field_labpoints',
      'field' => 'field_labpoints_value', // name of value field in that table.
      'value' => array('value' => userpoints_get_current_points()), // dynamic part.
      'operator' => '<',
      'id' => 'field_data_field_labpoints_vouchers', // unique id.
      'group' => 100, // new group.
    );
    $handler = views_get_handler($info['table'], $info['field'], 'filter');

    $handler->init($view, $info);
    $view->display_handler->handlers['filter'][$info['id']] = &$handler;

    unset($handler);
  }
}
  • 1
    Is this something you could do via a contextual filter (argument) and set in code? That way you wouldn't be interfering with the normal filter handlers and could do this programmatically. – Charlie Schliesser Jan 24 '13 at 15:41
  • I think that adding the filter as a contextual filter would be a good choise. – chefnelone Jan 28 '13 at 7:42
  • @CharlieS I just found a solution based in your comment. Do you mind to move your comment as an answer so that I can accept it. – chefnelone Jan 28 '13 at 13:26
3
+50

I would do this by adding a contextual filter, previously called an argument in D6 Views. You can set contextual filters / arguments in code when you embed a view. So, you could add a contextual filter for field_labpoints (not sure what kind of field this is) and pass along the argument you want to filter for when you embed the View in a .tpl file or a panel or whatever:

print views_embed_view('my_view_name', 'block_1', $labpoints_value);

This allows you to make the View more dynamic without tinkering with the existing filters.

1

Why not simply add your custom filter in a hook_views_query_alter? This way you'd be sure there's no conflict...

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.