Can I have a views field handler with an exposed form, or a filter handler with pre_query? 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?
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.