views:

92

answers:

1

I'm trying to create a Views Style plugin for the Views module in Drupal. The aim on the style plugin is to render the view as a drop down (i.e. SELECT) box.

I can't for the life of me find any decent or complete documentation on how to do this. I've looked at the source code of other Views Style plugins but that does not help.

Does anyone know how it's done or know any decent documentation, guides or tutorials on the subject?

+1  A: 

Not sure a style element is quite what you're looking for: even if you could get the form element to render as proper HTML, the security on the forms API is going to be be hostile to any values submitted from that element.

(Form elements in Drupal have a dual life: they exist as HTML s, but also in the form_state cache. So, any form element that isn't explicitly rendered by the forms API will be discarded when the form is submitted.)

A better solution, if you're looking for views-driven form elements, would be to build the form using the normal form API and have views populate the #options array of the element.

The function _nodereference_potential_references_views from the nodereference CCK add-on has a similar feature, and would probably be a good place to start. (It uses CCK hooks so you can't copy-paste directly in this case, but it should give you a pretty good sense of what you need to do)

anschauung
I'm not planning on using the forms API. It's just for a single dropdown box on the homepage of a website. The user will select from the list and then the dropdown will navigation to a new page.
Camsoft
I'm sure you have your reasons. But, I'd say your second sentence contradicts the first: this sort of thing is extremely quick and simple with the FAPI. With Views, it would be overkill at best -- views just wasn't built for generating interactive form elements.
anschauung
I'm no fan of Views' documentation, but your difficulty finding appropriate tutorials has a lot to do with the fact that you're using Views for something that Views wasn't meant to do.
anschauung
I was under the impression views style plugins are there to render views as HTML i.e. List, Table etc. You can't blame me in thinking that I can render the view as a HTML select box with a custom plugin? I'll have a look at Forms API. Thanks for your help.
Camsoft
No worries -- I'm sure it's possible to do it that way, just that it's probably the more cumbersome ways to do it: you'd end up completely rewriting several plugins for this one specific use. Come to think of it, doing this as a proof-of-concept this might be a good way to get more familiar with Views plugins, once you have a more traditional solution in place.
anschauung
You're absolutely right. I just used the Forms API and was so much easier. I'm quite new to Drupal to I'm still not quite sure what it's capable of hence why I went down the wrong route. I used the function `views_get_view_result` to get the data from the view and used that to populate the options argument of the SELECT field type. It's working like a dream now. Thank you so much for your help.
Camsoft
Glad it you were able to work it out :)
anschauung