2

It's basically in the title, but here's the question:

I'm trying to create a way to allow the user looking at a view to control which columns are visible. This isn't very hard, provided I can get called in the right place. Specifically, I need to be called in three places:

  • in some kind of configuration pane
  • in the exposed form
  • in the pre_render function

Both field handlers and filter handlers get a configuration pane. But only filter handlers get to affect the exposed form, and only field handlers get access to the pre_render function.

Although I might be able to get something working by combining a handler with various hooks, I would prefer everything to be together.

Does anyone know how to convince Views to either call a field handler when rendering the exposed form, or to call a filter handler for the pre_render function?

1

If I understand your question correctly, here's how I would go about this:

What you need to do is create a base node view. This should have all fields which are present regardless of other options selected. You'll want at least one exposed filter, so that views will render a form which you can hook into for form alter. Your process for modifying the view on the fly will be two fold.

  • The first will be a hook_form_alter() in which you make any modifications needed to the exposed views form. Add in any custom form elements which you'll use to control the fields displayed in the view. In your submit handler, you'll need to stuff the form values into session, so you can access them later.

  • The second is to use hook_menu_alter to override the default page handling in views and provide your own callback. In that callback you'll need to programmatically load and display the view. You can also add/remove additional fields to be displayed on the fly by calling $view->override_option('fields', array($some_fields));. I recommend looking at exported views to get the proper format for this, as anything you see in there is valid to do before executing the view. After you've added the fields you need, just return $view->execute_display(); to render the view. You can see an example of where I do this at http://890234892342.privatepaste.com/6b72c3e011 -- In this case, I'm only swapping out views displays / filters / arguments on the fly, but the exact same method will work for adding/removing fields. Again -- looking at exported views code is a great method to understand how you can swap this stuff out on the fly. When trying to figure out the format for modifications I need to make, I actually modify the view, export it, look at the exported code, and then revert it back to what it was originally. This allows me to see how views is formatting various fields and what array I need to pass into $views->override_options() to get the desired behavior.

It's not a particularly easy process to either explain nor do, but this is an effective method for programmatically altering views on the fly. Feel free to ask followup questions if there is something you don't understand.

  • Hi! I think this is a bit excessive for my needs -- I can do what I want to do fairly easily by changing the 'exposed' value for a field in the pre_render() call. I think I can actually do this with hook_form_alter and hook_views_pre_render; I'm just trying to add the filter. However, I may take a look at the override_options call -- that may help... – Ricky Morse Mar 16 '11 at 0:53
0

It turns out that the view object has a property for each class of handlers that should be called. So to take a filter handler and have it be called as a field, you need to add it to the field array:

function query () {
    $this->view->field[$this->options['id']] = $this;
}

Your filter will then get called as a field. I found it useful, after I was done, to remove the filter from the set of field handlers:

function pre_render ($values) {
    // do stuff here

    unset($this->view->field[$this->options['id']]);
}

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.