Hello Views community,

I'm currently looking for a very specific implementation of a views exposed filter. Let's say we have a content-type which uses a taxonomy free-tagging style. I want a view showing every node of that type. At the top there should be a list of all active tags next to each others (if need be in several lines). The user now can click one or several of these tabs, marking them as selected, and the view automatically refreshes showing only nodes that contain this or these tags (maybe there should be a selection to choose if you want an AND or an OR selection). AFAIK there is no way of doing this with the currently available views modules. Or is there?

If there isn't, my next question would be, how can I make this happen? I've already written some minor drupal modules, so I know my way round forms api, hooks etc. I'd just need someone to point me to the documentation I should be reading to be able to accomplish this task. Like where is the view 2.x api documentation and which specific hooks should I be looking at. If something useful evolves from this project, I'll gladly share it with the community.

Thanks for your help and thanks for a really outstanding module.

Dave

Comments

merlinofchaos’s picture

Status: Active » Fixed

Wow. I think this sounds like it would be crazy difficult.

The Views API documentation is linked from the Views project page and is easy to get to. My best guess is that you would start by adding a new taxonomy term filter handler that uses the form you want to use. To add this handler, you would probably use hook_views_data_alter() and the handlers in question are defined in taxonomy.views.inc -- the rules for defining handlers are laid out (though a little bit obtusely) in the advanced help under the handlers page.

You should particularly look in taxonomy.views.inc for its usage of hook_views_data_alter and see how it adds filters to the node table. I imagine you'll do something specific.

Also, taxonomy is amongst the most difficult handlers in all of Views. Sorry about that, but taxonomy's data storage mechanism works really well for a couple of things and sucks a lot for everything else.

Good luck!

huesforalice’s picture

Thanks Merlin!

It will some time before I start development on this one, but I'll probably be back with some more questions then.

NoRandom’s picture

huesforalice, maybe you should take a look at http://drupal.org/project/taxonomy_filter . It's not as powerful as you want and it only allows you to use multiple tags in AND mode (not OR) but maybe you could improve the module.

Here a little example of the module working (filters at the bottom, this is configurable in the module) and custom php code in the header to show tags with a better style (in my opinion).

http://pixelgordo.com/taxonomy/term/73,153

Regards

huesforalice’s picture

Thanks for the tip, I will definately look into that module!

Dave

huesforalice’s picture

Status: Fixed » Active

Ok I've started working on this, but I'm having a hard time getting my head around certain things. This is what I have so far:

viewtagging.views.inc

function viewtagging_views_data()
{
	return NULL;
}

function viewtagging_views_plugins()
{
	return NULL;
}

function viewtagging_views_data_alter(&$data)
{
  $data['node']['term_tags'] = array(
    'group' => t('Taxonomy'),
    'title' => t('Term Tagging'),
    'help' => t('Activate Tagging for your term ids.'),
	'filter' => array(
      'handler' => 'viewtagging_handler_filter_term_tags',
    ),
  );
}

function viewtagging_views_handlers ()
{
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'viewtagging'),
    ),
    'handlers' => array(
      'viewtagging_handler_filter_term_tags' => array(
        'parent' => 'views_handler_filter_many_to_one',
      ),
    ),
  );
}

viewtagging_handler_filter_term_tags.inc

<?php

/**
 * Taggable views
 */

class viewtagging_handler_filter_term_tags extends views_handler_filter {

  //filter has extra options
  function has_extra_options() { return TRUE; }

  function extra_options_form(&$form, &$form_state) {
  
    $vocabularies = taxonomy_get_vocabularies();
	
    foreach ($vocabularies as $voc) {
      $options[$voc->vid] = check_plain($voc->name);
    }

      $form['vid'] = array(
        '#prefix' => '<div class="views-left-40">',
        '#suffix' => '</div>',
        '#type' => 'radios',
        '#title' => t('Vocabulary'),
        '#options' => $options,
        '#description' => t('Select which vocabulary to show terms for in the regular options.'),
        '#default_value' => $this->options['vid'],
      );
	  
  }
  
  //override query function for now
  function query () 
  {
  }
 
  function exposed_form(&$form, &$form_state) { 
	//selected vid
	$vid = $this->options['vid'];
	
	//get all terms
	$voc = taxonomy_get_tree($vid);
	
	//render a button per term
	foreach ( $voc as $term )
	{
		$form['terms']['term_'.$term->tid] = array(
		'#type' => 'submit',
		'#value' => t($term->name),
		'#disabled' => false,
		);
	}
  }
	
  //This filter is always exposed, wouldn't make any sense if it weren't	
  function can_expose() { return TRUE; }
  function is_exposed() { return TRUE; }

}

If I activate this in views, it renders out a button for every taxonomy term that is available. The user should be able to click one of the buttons to "switch" this term und and off.

My questions are:
1) Does the code make any sense so far?
2) Can I use the table definitions of the views.taxonomy modules somehow without having to copy them into my module?
3) Should I be extending something like views_handler_filter_many_to_one? I'm not sure it suits my needs in terms of the user interface.
4) I don't understand how I can access (read and write) the options of the exposed filter. With that I mean the ?myfilterops=tag1/tag2/tag3 part of the url.

Thanks once again for your help.

esmerel’s picture

Status: Active » Needs review

Bumping for potential review.

merlinofchaos’s picture

Status: Needs review » Postponed (maintainer needs more info)

Hm. I don't think this really should be needs review. I"m not actually sure what to do with this, either.

esmerel’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)

OK, there's been no updates for a while; I'm going to go ahead and close it. if someone wants to take it back up, that's totally fine.