7

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?

Camsoft
  • 11,718
  • 19
  • 83
  • 120
  • 1
    Did you see http://groups.drupal.org/node/10129 under "Writing Views 2 style and row plugins"? Instead of simply asking how it's done, could you post your best effort code and/or tell us exactly which problems you're running into? – marcvangend Sep 13 '10 at 19:15
  • and after you can write the "decent" documentation.... thanks – gagarine Sep 13 '10 at 22:18
  • For what it's worth, I agree that Views documentation is needs a lot of work -- for such a complex module, there's terribly little information about how it's supposed to operate. The best bet is to look around for a module that does roughly what you want to do (see my example below) and figure out what they did. – anschauung Sep 13 '10 at 23:55
  • @marcvangend I had looked at that article but did not understand it. I was not asking someone to write my code for me I asked if anyone knew of any decent documentation. Is that too much to ask for such a complex module? – Camsoft Sep 14 '10 at 13:54
  • @gagarine I would happily write documentation if I knew about to use it! – Camsoft Sep 14 '10 at 13:54

1 Answers1

1

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
  • 3,697
  • 3
  • 24
  • 34
  • 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 Sep 17 '10 at 13:19
  • 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 Sep 17 '10 at 18:56
  • 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 Sep 17 '10 at 18:58
  • 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 Sep 20 '10 at 09:52
  • 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 Sep 20 '10 at 09:58
  • 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 Sep 20 '10 at 11:31