Basic Searching with Views
Requirements
Requirements: Imported listings, Views 3.x
Setting Up a View
- Create a new View
- 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.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion