I am trying to filter my view by a computed field. My main issue is that the computed field only offers these operators:

  • Is Greater Than
  • Is Greater Than Or Equals
  • Is Equal To
  • Is Not Equal To
  • Is Less Than Or Equals
  • Is Less Than

and then a text field. I am looking to make the text field a drop down. I have been able to do this by creating a block using the following code and displaying on the corresponding view:

<form id="views-filters-block" method="get" action="http://localhost/acedesigns.com/capablitites/framing/mouldings/blacks">
<div class="form-item-0">
 <select id="edit-filter0" class="form-select" name="filter0"><option value="**ALL**">-all-</option><option value="black">black</option><option value="silver">silver</option><option value="gold">gold</option><option value="brown">brown</option><option value="white">white</option><option value="other">other</option></select>
</div>
<div class="form-item1">
 <select id="edit-filter0" class="form-select" name="filter1"><option value="**ALL**">-all-</option><option value="yes">yes</option><option value="no">no</option><option value="maybe">maybe</option></select>
</div>
<div class="form-submit">
<input type="submit" class="form-submit" value="Submit" id="edit-submit"/>
</div></form>

I then hide the regular filter created by the view with:

/* This is a hack to remove the filter table above each view.  NOTE: This removes ALL view filters!!*/
form#views-filters{
  display:none;
}

I would like to:

  1. how do I make the selected option change?
  2. is there a prettier way to do this?

thanks!

Comments

mooffie’s picture

is there a prettier way to do this?

Yes. Starting with Views 1.6 you can implement hook_views_tables_alter, in a custom module, to alter the definitions of filters. E.g.:

function mymodule_views_tables_alter(&$tables) {
  $tables['node_data_field_color']['filters']['field_color_value_default'][...] = ...;
}

(I assumed the name of the field is "Color".)

A very detailed explanation of all the members in a filter array can be found here (especialy "part 5" and "part 2").

pinxi’s picture

It would have helped if I posted the original code:

<form id="views-filters" method="get" action="http://localhost/acedesigns.com/capablitites/framing/mouldings/blacks">
<div><table>
 <thead><tr><th/><th/><th>cas</th><th/> </tr></thead>
<tbody>
 <tr class="odd"><td><div class="form-item">
 <input type="text" class="form-text" value="" size="60" id="edit-filter0" name="filter0" maxlength="255"/>
</div>
</td><td><div class="form-item">
 <input type="text" class="form-text" value="" size="60" id="edit-filter1" name="filter1" maxlength="255"/>
</div>
</td><td><div class="form-item">
 <select id="edit-filter2" class="form-select" name="filter2"><option value="**ALL**"><All></option><option value="black">black</option><option value="silver">silver</option><option value="gold">gold</option><option value="brown">brown</option><option value="white">white</option><option value="other">other</option></select>
</div>
</td><td><input type="submit" class="form-submit" value="Submit" id="edit-submit"/>
</td> </tr>
</tbody></table>
</div></form>

This is the way that the current filters print. I want to remove them from the page, move them to a side block and display them in divs.

I found this from Merlin and it works perfectly to remove the filters from the page: (added to template.php)

  function phptemplate_views_display_filters_moulding_blacks() {
    // Do absolutely nothing.
  }

However, when I add the accompanying code to display the filters in a block. My site breaks (I think it is for Drupal 4.7):

  $view = views_get_view('moulding_blacks');
  $form = views_filters_form($view);
  $form['#action'] = url($view->url);
  return drupal_get_form("views_filters_$view->name", $form, 'views_filters');

The main issue is that this is all a little over my head, but I am learning!

Thanks

pinxi’s picture

I installed the filter block module and then added this code to my template:

 function phptemplate_views_filterblock($form) {
$view = $form['view']['#value'];
foreach ($view->exposed_filter as $count => $expose) {
    $label = '<div class="form-element-label">'.$expose['label'].'</div>';
    $formelement = drupal_render($form["op$count"]) . drupal_render($form["filter$count"]);
    $o .= '<div class="form-element-'.$count.' form-element-'.$expose['label'].'">'.$label.$formelement.'</div>';
}
$o .= '<div class="form-submit-button">' . drupal_render($form['submit']) . '</div>';
return $o . drupal_render($form);
}

This gave me the filters in the side block in div's without collapse/uncollapsed (like I want). Now, I just need to figure out how to change the text field inputs into drop downs.

I am trying to do something like this to no avail:

/** Now we edit our tables for calculated fields.  **/
function mymodule_views_tables_alter(&$tables) {
  $tables['node_data_field_moulding_width_category']['filters']['field_moulding_width_category_value'] = array(
				'#type' => 'select',
				'#options' => array(
					'***All***' => t('all widths'),
					'3' => t('> 3'),
					'2to3' => t('>2 <= 3'),
					'1to2' => t('>1 <= 2'),
					'1' => t('<= 1')
				)
				);
}
notarealperson’s picture

Were you able to figure this out?

pinxi’s picture

Here is an example of what I am trying to do:

http://ace.pinxi.net/capabilities/framing/mouldings

socialnicheguru’s picture

subscribing

http://SocialNicheGuru.com
Delivering inSITE(TM), we empower you to deliver the right product and the right message to the right NICHE at the right time across all product, marketing, and sales channels.

colan’s picture

Subscribing.

esmailzadeh’s picture

subscribing