6

Currently I have a views exposed filter for a "City" field. Typical output looks like:

<option value="Liverpool">Liverpool</option>

I would love to be able to add a class to this such as:

<option value="Liverpool" class="north-west">Liverpool</option>

I currently have a CCK field "field_region" which is set to "North West" for Liverpool - so it would be brilliant if anyone had any suggestions with how to achieve this on my exposed form input.

Cheers, Garry.

  • Do you use Drupal 6? – user1513 Jun 24 '11 at 14:15
  • Yes this is using Drupal 6 - even if I could add a class of the same value for example: <option value="liverpool" class="liverpool">liverpool</option> - this would be a good start – Garry Jun 28 '11 at 13:52
  • I'm looking for exactly the same. Too bad this possibility isn't baked into the Form API already. Did you post a feature request in the Drupal issue queue, or look for an existing one? – marcvangend Jun 30 '11 at 9:39
  • There is a css selector based on attributes. Try option[value="Liverpool"] – Capi Etheriel Dec 11 '11 at 6:50
5
+50

Normally this would be accomplished with hook_form_alter, but sadly Drupal 6 doesn't support attributes in the options Form API element, which means you can't add any classes to it.

You're going to have to do this with Javascript. :-(

You can't start with something like this:

  $('.view-NAME_OF_YOUR_VIEW .views-exposed-widget option').each(function(index) {
    $(this).addClass($(this).val());
  });

This will get you

<option value="Liverpool" class="Liverpool">Liverpool</option>

I know it's not perfect, but sadly that's probably the simplest route.

3

Hm. You could always implement your own theme_select(), and them from it call your implementation of form_select_options() - in which you can do whatever you want with your options...

  • You can do that, but that would be a lot of work, mainly because then you're stuck rewriting the form_select_options and you risk breaking other options elements because you'll need to turn the options into more than key|value pairs. I wouldn't recommend this. – ericduran Jun 29 '11 at 21:01
  • Instead of rewriting theme_select you're better off implementing your own form api element. Which is still a lot of work, for the select options. – ericduran Jun 29 '11 at 21:02
  • 1
    Actually, a combination of theme_select() + form_select_options() is not a lot of work at all, as it's enough to just copy both functions, change their names and do a small update to the latter. In terms of risk, it all depends how you do it. If you try to add extra params to options array to use them in the form, then yes, I agree, this could be risky. On the other hand, if you do not change options passed to form, but just add extra classes directly in new form_select_options(), based for example on option key (or reformatted text) - then you do not risk anything at all. – Maciej Zgadzaj Jun 29 '11 at 21:53
  • yes, but that solution wouldn't be much help because it would only work with this one problem and it wouldn't be very flexible. :-D we can agree to disagree :-) – ericduran Jun 29 '11 at 22:48

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.