Hello,

I made a view using a form in a block.

That form is a select box.

I would like to have, instead of a select box, a list of links.

I've installed the devel module to spot which file/function is called and it is the template views-exposed-form.tpl.php.

<?php
// $Id: views-exposed-form.tpl.php,v 1.4 2008/05/07 23:00:25 merlinofchaos Exp $
/**
 * @file views-exposed-form.tpl.php
 *
 * This template handles the layout of the views exposed filter form.
 *
 * Variables available:
 * - $widgets: An array of exposed form widgets. Each widget contains:
 * - $widget->label: The visible label to print. May be optional.
 * - $widget->operator: The operator for the widget. May be optional.
 * - $widget->widget: The widget itself.
 * - $button: The submit button for the form.
 *
 * @ingroup views_templates
 */
?>
<?php if (!empty($q)): ?>
  <?php
    // This ensures that, if clean URLs are off, the 'q' is added first so that
    // it shows up first in the URL.
    print $q;
  ?>
<?php endif; ?>
<div class="views-exposed-form">
  <div class="views-exposed-widgets clear-block">
    <?php foreach($widgets as $id => $widget): ?>
      <div class="views-exposed-widget">
        <?php if (!empty($widget->label)): ?>
          <label>
            <?php print $widget->label; ?>
          </label>
        <?php endif; ?>
        <?php if (!empty($widget->operator)): ?>
          <div class="views-operator">
            <?php print $widget->operator; ?>
          </div>
        <?php endif; ?>
        <div class="views-widget">
          <?php print $widget->widget; ?>
        </div>
      </div>
    <?php endforeach; ?>
    <div class="views-exposed-widget">
      <?php print $button ?>
    </div>
  </div>
</div>

I'm trying to understand how to change that select box but it's coming from the variable $widget->widget and I don't know how to change it...

I'm using D6.12 and Views 2.5.

Thanks for your help.

Comments

harkaman’s picture

This here talks about theming the views exposed filter: http://drupal.org/node/262270

And, here is the reference to the views API: http://views.doc.logrus.com/

I am trying to accomplish this myself, however, if you already have please let me know.

Thanks.

Chris Einkauf’s picture

Here's what I did with my views-exposed-form.tpl.php to get this functionality working...

(In my situation, I had multiple filters for my view, and wanted the options for a given filter to go away if an option was chosen for that filter. I'm not using ajax for my exposed filters, and this code won't work for someone who wants to use ajax with their exposed filters. It basically works by creating your list of filter options, and then turning each one into a link that appends that argument onto the url. Hopefully, this code will serve as a starting point for someone else looking to have a similar functionality. Any improvements/modifications to this code are more than welcome.)


<?php
// $Id: views-exposed-form.tpl.php,v 1.4 2008/05/07 23:00:25 merlinofchaos Exp $
/**
 * @file views-exposed-form.tpl.php
 *
 * This template handles the layout of the views exposed filter form.
 *
 * Variables available:
 * - $widgets: An array of exposed form widgets. Each widget contains:
 * - $widget->label: The visible label to print. May be optional.
 * - $widget->operator: The operator for the widget. May be optional.
 * - $widget->widget: The widget itself.
 * - $button: The submit button for the form.
 *
 * @ingroup views_templates
 */
?>
<?php if (!empty($q)): ?>
  <?php
    // This ensures that, if clean URLs are off, the 'q' is added first so that
    // it shows up first in the URL.
    print $q;
  ?>
<?php endif; ?>

<?php 
	 
	
  foreach ($form['#info'] as $id => $info) {
	
	$thisfilter = new stdClass;
	
	if (isset($form['#post'][$info['value']])) {
		$existargs = TRUE;
		$urlargs .= $info['value'] . '=' . $form['#post'][$info['value']] . '&amp;';
		unset($widgets[$id]);
	} else {
	
		// set up defaults so that there's always something there.
		$thisfilter->label = $thisfilter->operator = $thisfilter->filteroutput = NULL;
		if (!empty($info['label'])) {
		  $thisfilter->label = $info['label'];
		}
		if (!empty($info['operator'])) {
		  $thisfilter->operator = drupal_render($form[$info['operator']]);
		}
		
		$filteroptions = $form[$info['value']]['#options'];
			
		foreach ($filteroptions as $thisoption) { 
			$thisoptiondetail = $thisoption->option;
			foreach ($thisoptiondetail as $optionid => $optionterm) {
				if (!($existargs)) {
					$thisfilter->filteroutput .= '<a href="' . $_GET['q'] . '?' . $info['value'] . '=' . $optionid . '">' . $optionterm . '</a><br />';
				} else {
					$thisfilter->filteroutput .= '<a href="' . $_GET['q'] . '?' . $urlargs . $info['value'] . '=' . $optionid . '">' . $optionterm . '</a><br />';
				}
			}
		}
		
		$widgets[$id]->widget = $thisfilter->filteroutput;
	}
	
  }
  
?>
	
<div class="views-exposed-form">
  <div class="views-exposed-widgets clear-block">
    <?php foreach($widgets as $id => $widget): ?>
      <div class="views-exposed-widget">
        <?php if (!empty($widget->label)): ?>
          <label>
            <?php print $widget->label; ?>
          </label>
        <?php endif; ?>
        <?php if (!empty($widget->operator)): ?>
          <div class="views-operator">
            <?php print $widget->operator; ?>
          </div>
        <?php endif; ?>
        <div class="views-widget">
          <?php print $widget->widget; ?>
        </div>
      </div>
    <?php endforeach; ?>
<!-- get rid of the button
    <div class="views-exposed-widget">
      <?php //print $button ?>
    </div>
-->
  </div>
</div>


Chris Einkauf’s picture

FYI for all of the people reading this... there's a discussion over in this thread about this code and how it requires the GET method which produces long URLs (i.e. loaded with variables) and doesn't work with AJAX.

Gabriel R.’s picture

Hi Chris,

this code seems like the cleanest, most elegant way to have a list of links in instead of select boxes in exposed filters. I scanned the forums and issues queue but nothing very useful.

Unfortunately, this doesn't work for me. The exposed filters disappear when the template file is created and the views template folder re-scanned.

Any suggestion would be appreciated.

Thanks.

muschpusch’s picture

For drupal 7 better_exposed_filters supports links as exposed filters!