creating a custom module to override the default exposed filter widget type?

Events happening in the community are now at Drupal community events on www.drupal.org.
stevepofd's picture

Hello,
I'll start with the good ol' i'm new to drupal and creating custom modules spiel...
(btw, im using the most current versions of drupal and views)
What i need/have been trying to do is create a filter for a view that uses select dropdowns based on values that are collected from a cck text field.
(for example: a user adds a new node, there is a field called type and the user can enter whatever he wants. when the form is submitted, and he goes back to the list view, his entry in the type field now shows up in a select dropdown along with all of the other options that other users added. now he can filter the view with his own type entry.)

I know you must be thinking, 'why dont you just use the cck select widget and specify values to be selected when creating the node?' Obviously because that is not what the client wants, and they refuse to budge.

One of my first thoughts was to use taxonomy to achieve this, however after trying a few things it seemed a little less flexible and moreover my client wasn't thrilled about the data entry (well, the 'workflow' of the form to add their data).

I have tried looking through a couple modules that interact with views (date,calendar,flags) and i have even looked a lot through the official views docs but i cannot seem to get the concept of it. maybe i am just not opening my eyes enough or i am too new to the whole system.

Another idea i had was to see if there was a method to manually select the widget that will be used on an exposed filter. Again, maybe i am way off. If anyone has some insight in the best method to achieve this i'd be forever greatful.

I do have experience in using php classes and quite a bit of experience with mysql, so building queries wont be so bad, but even after a couple books drupals working still get my head spinning...

Thank you in advance to everyone for your help!
Take care,
steve

Comments

exposed textfield filter dropdown widget

dboune's picture

Hi Steve.. I'm about to start working on the same thing. If you have learned anything I'd love to hear about it. Maybe we could collaborate? I'll post anything I learn and any progress here for the time being.

Found this request in the

dboune's picture

Found this request in the CCK queue.. not quite what I'm looking for: http://drupal.org/node/338994

Will need to allow dropdown for textfields with no specific allowed options. This does however point me toward CCK for the moment, since CCK exposes the allowed values list to views in some manner. Could be that I just need to do the same for table data.

I found it easy enough to provide the needed View admin UI options, for the moment in the views_handler_filter_string class (for experimentation purposes). Getting it to pull the proper list or render is another matter. The render process isn't clear to me yet. However, this may be moot if the simple solution is wholly in CCK.

didn't get far but...

stevepofd's picture

hey dboune,
i haven't had much time to get this rolling, i lost a lot of time researching and when time came i ended up using the taxonomy method i originally spoke about. recently, i got this lead which sparked my interest:
http://drupal.org/node/463990
i think this has something to it, as well i was thinking about it the other day while browsing through the views ajax files and it seems that getting a widget into the form is the key and hook_form_alter seems like a good way to go about it.
i would be more than happy to collaborate, i'll be honest i've never collaborated on any drupal projects yet, but i think this could be a good exercise for really getting the most out of my clients needs and the view module.
i'll keep you posted here as well if i come up with more... please do the same as everytime i have something to think about i feel like i get a little closer.

take care,
steve

Module version

dboune's picture

here is the module version: http://drupal.org/node/463990#comment-1612804

First attempt...

dboune's picture

So.. I haven't been trying to write a custom module to try to do this. My thoughts are that something like this needs to make progress getting into CCK/Views itself.. So far, just CCK.

Tooling around today, I made something work. I'm sure someone would have terrible things to say to me about how I did it, but alas, I'm just learning when it comes to the guts of CCK and Views.. so.. disclaimer: probably did this all wrong and terrible... feedback, guidance, pointers, code; greatly desired.

I'll generate a diff later. For now here is the modified code. Really a very simple set of changes. I admit to being a bit lazy; I just don't yet know how this will turn out in the end. Someone got a flashlight?

modules/text/text.module : function text_field_settings

    case 'views data':
      
      $data = content_views_field_views_data($field);
      $db_info = content_database_info($field);
      $table_alias = content_views_tablename($field);

      // Filter: Add a 'many to one' filter.
      $copy = $data[$table_alias][$field['field_name'] .'_value'];

      $allowed_values = content_allowed_values($field);
      if (count($allowed_values)) {
        $copy['title'] = t('@label (!name) - Allowed values', array('@label' => t($field['widget']['label']), '!name' => $field['field_name']));
      } else { 
        $copy['title'] = t('@label (!name) - Available values', array('@label' => t($field['widget']['label']), '!name' => $field['field_name']));
      }
     
      $copy['filter']['handler'] = 'content_handler_filter_many_to_one';
      unset($copy['field'], $copy['argument'], $copy['sort']);
      $data[$table_alias][$field['field_name'] .'_value_many_to_one'] = $copy;
      // Argument : swap the handler to the 'many to one' operator.
      $data[$table_alias][$field['field_name'] .'_value']['argument']['handler'] = 'content_handler_argument_many_to_one';
      return $data;

includes/views/handlers/content_handler_filter_many_to_one.inc

  function get_value_options() {
    $this->value_options = $this->allowed_values();
    if (!count($this->value_options)) {
      $db_info = content_database_info($this->content_field);
      $result = db_query("SELECT DISTINCT " . $db_info['columns']['value']['column'] . " FROM {" . $db_info['table'] . "}");     
      while ($row = db_fetch_array($result)) {
        $this->value_options[$row[$db_info['columns']['value']['column']]] = $row[$db_info['columns']['value']['column']];
      } 
    }
  }

In views, select the variant of the field you want to filter on marked "Available Values".

I have NO idea if I've goofed up anything else with this, or what scenarios will break. It seems to work fine with a regular CCK textfield.

Enjoy..

trying your work out

stevepofd's picture

alright, i got all of your changes in place, i didn't have time to go through with a fine tooth comb, but it seemed like things might work out.
i didn't get any errors so things seemed to go well. i created a new content type and a text field (like i would normally). still no problems.
i then created a new view and added things like normal. when creating the view and exposing the version of the field with "available values" i still hd no problems except i got a select list instead of drop down. is that what you were going for? maybe i lost track of my thoughts and thats what you were going for. (i also dont remember this being an option before making your changes, so it's seems to be on the right track).
i was looking through how i set up the taxonomy the work as drop downs and i noticed that when first selecting the field to expose the filter, it gave me an option to select a dropdown or the autocomplete widget. i have no idea how that works into to play because it didn't prompt me with these options with your implementation (i also dont imagine it was your aim, but maybe an interesting idea)...
i attached some images of what i did so you can see what i experienced with it.
i would love to test more and see if i could figure something out, but i just dont have a ton of time.
please let me know if i missed something...

take care,
steve

Dropdown vs Select

dboune's picture

What is the difference for you, between a "dropdown" and a "select"? Are you looking for an "Autocomplete" field? Personally, I needed an HTML Select field (which is what I refer to as a "Dropdown").

I would love it if we could add a selector for Dropdown versus Autocomplete.. I know how to do that on the views side, but really, it seems to me that this would have to be / should be, done on the CCK side. I just don't know if it is really in my power to do so.

Once I've taken a few of the other issues off my list (exposed sorts, Select based numeric BETWEEN), I'll return to this and see what more I can do.

Note the module version of this code (linked above). The ability to make an Autocomplete field out of it there, may be much more tangible in terms of time to implement and you would not be "killing kittens" while, hopefully, I can see if some of this work might actually make it into core. crosses fingers.

i'm stupid thats why!

stevepofd's picture

we are on the same page about what a dropdown/select field are... i am just stupid and selected the wrong options for the select field. i wasn't thinking and when you allow multiple values to be selected it gets displayed like the pic i posted.

so i would have to say great work!
that does exactly what i had originally set out to do.

autocomplete may be nice in the future but so far no client has asked for it... so let's save some kittens!

again, thank you. i hope you know how much your contributions mean to the community!

i'm still game to help on issues surrounding this once i have a free moment.

take care,
steve

w/ Autocomplete

dboune's picture

I found a reason to implement autocomplete.. so.. because it was so much more simple as a module, I did it there first:

http://drupal.org/node/463990#comment-1616402

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week