Requirements

Requirements: Imported listings, Views 3.x

Setting Up a View

  1. Create a new View
  2. Select 'Drealty Listing' as the type of View

Whether you pick page or block, continue with the normal setup of a View.

Like any normal View, add the fields you want to see (we highly recommend View Modes instead of standard field output), and any filters to set the filter criteria.

A basic view will contain exposed filters, such as Price, MLS ID (rets_key or rets_id), and City. You may notice however, the Views search filters display less than desired: typically basic text fields. For MLS ID, this is fine, since a basic 'contains' filter works here. But for Price and City? You really want a drop down of select options, right? That is better for the end user.

Form Alter Your View Filter Form

There is a way to make text fields become select drop downs with options, however. Lets start with City, since that is easy. In a custom module, write a form alter hook:


function your_custom_module_form_alter(&$form, &$form_state, $form_id) {
   if ($form['#id'] == 'YOUR-CUSTOM-VIEW-ID') {

   }
}

Note in the above, we have to use $form['#id'] to get the actual ID of the form, otherwise, we only get views-exposed-form from $form_id.

Now, lets get our options for cities:


function your_custom_module_form_alter(&$form, &$form_state, $form_id) {
   if ($form['#id'] == 'YOUR-CUSTOM-VIEW-ID') {
     $cities = db_query('SELECT DISTINCT(field_YOURCITYFIELD_value) FROM {field_data_field_YOURCITYFIELD} ORDER BY field_YOURCITYFIELD_value ASC', array())->fetchAll();
	
     $city_options[''] = '- Select a city -';
  
     foreach ($cities as $record) {
       $city_options[$record->field_YOURCITYFIELD_value] = $record->field_YOURCITYFIELD_value;
     }

     unset($form['field_YOURCITYFIELD_value']['#size']);
     $form['field_YOURCITYFIELD_value']['#default_value'] = '';
     $form['field_YOURCITYFIELD_value']['#type'] = 'select';
     $form['field_YOURCITYFIELD_value']['#options'] = $city_options;
   }
}

The above code should transform your basic text field into a select drop down with cities as options, from A to Z. We get our city options from drealty city field that you setup (the name will vary on what you entered). Ideally we would like our city option array to be stored as a variable or cache data so we aren't hitting the database on each page request, but for now, we just want to make it work.

When the form submits, the plaintext value is submitted (being the value on the option selected) and passed into Views, who doesn't care what kind of field type it is as long as the data matches the criteria.

Getting a Price Range Filter Field

This is one of the more harder things to setup with Views, and there are a couple of ways to do it.

Comments

j9’s picture

Thanks very much for writing this howto.

I do not understand what you mean by this:

In a custom module, write a form alter hook:

Is this something we can do through the Views interface?

Do we need another module to do this? Do we have to write our own Drupal module to have drop down menus?

Okay! Thanks and Im looking forward to learning how to implement the dropdown menus! :0)

j9’s picture

Would you be able to share your custom module (and how to implement it).

I do not know if this means a module that shows up in the drupal Modules page?

mgriebe’s picture

Thanks!

BTW, this

     unset($form['field_YOURCITYFIELD']['#size']);
     $form['field_YOURCITYFIELD']['#default_value'] = '';
     $form['field_YOURCITYFIELD']['#type'] = 'select';
     $form['field_YOURCITYFIELD']['#options'] = $city_options;

should be

   unset($form['field_YOURCITYFIELD_value']['#size']);
     $form['field_YOURCITYFIELD_value']['#default_value'] = '';
     $form['field_YOURCITYFIELD_value']['#type'] = 'select';
     $form['field_YOURCITYFIELD_value']['#options'] = $city_options;

I'm learning loads about drupal setting up this site and you guys in the community are a force_multiplier.

pengie’s picture

I've been able to get most of the fields working properly, however, there are 2 things I just can't figure out. I can get radios to work as the type, but not checkboxes. I can get checkboxes to display, but when anything is checked, the filter doesn't display any results. This is simply by changing the filter to filter for contains any and changing the type to "checkboxes."

The next thing I can't figure out for the life of me is getting the options to read such as Bathrooms: 1, 2, 3, 4, 5, 6+
I was able to do it as 1+, 2+, 3+ and so forth, but this isn't ideal. I made an attempt at some code using in_array, but well, it doesn't work.