Hi, I am trying out the Apachesolr Views module and created a nice view with it. My facet block with cck dates already works perfectly.
But now i want to limit the results to all nodes with a date value after a specified date. So for example, all books released after 2009.
What would be the best way to make this possible and make it reuseable for others?

Comments

toemaz’s picture

You could use hook_apachesolr_modify_query(). See also #632962: Using hook_apachesolr_modify_query()

/**
 * Implementation of hook_apachesolr_modify_query
 */ 
function YOUR_MODULE_apachesolr_modify_query(&$query, &$params, $caller) {
   // Do the magick here
}
Maikel’s picture

thanks for your quick reply!
Would this be a total filter, or a configureable filter within Apachecsolr Views? I was also looking at creating a filter handler for this module, could that be a possible solution too?
Thanks again.

toemaz’s picture

Category: support » feature

The above snippet is to alter the query. It's not a configurable filter.

For creating a configurable filter, you need to implement 2 hooks
hook_views_handlers()
hook_views_data()
and most likely implement a handler which extends one of the views_handler_filter subclasses.

There is no date filter for apachesolr_views yet, so I would put this now under a feature request. So, try to look at how Scott has made the other filter handlers and try perhaps to extend the views_handler_filter_date class.

Maikel’s picture

Category: feature » support

Allright,
I have implemented:
hook_views_handlers()
hook_views_data()

By hacking apachesolr_views.views.inc for testing, when it works out ill create a seperate module or something.

My current data part looks like this:

$data['apachesolr_' . $base_table][apachesolr_index_key($field)] = array(
  'title' => t($field['label']),
  'help' => t('CCK Mapping for @fieldname', array('@fieldname' => $field['field_name'])),
  'filter' => array(
    'handler' => 'views_handler_filter_date',
    'cck_field' => $field,
  ),
);

And it does this only for fields of the type 'datetime'.

Now, as you can see i just used the existing views_handler_filter_date. This gives me the filter, and the option to enter in the date i wish to filter on, but after doing that i get the following error:

Fatal error: Call to undefined method apachesolr_views_query::add_where() in public_html/sites/all/modules/views/handlers/views_handler_filter_date.inc on line 151

Now i am a bit lost in what to do. I looked at other apachesolr_views_handler_filter_*.inc files and saw most of them extended an existing class, ofcourse i can extend the extisting date_handler but i still have no clue about what to do to prevent the same error from happening.

toemaz’s picture

Well you can't use views_handler_filter_date but instead you need to extend it and make a date filter for apachesolr_views. Check out how the other filter handlers are extending the default views handlers and override the query() function. So this work is a little OO coding, rather than the usual procedural coding.

I'm not a guru on this matter, but I have been trying to do the things you are doing now with my own CCK fields. However, that didn't include a date field yet. Sorry I can't help any further.

scotjam’s picture

Hi, I'm looking to do something really similar to adding a custom filter, but to reflect different currencies, i.e. a drop down list of three options 'GBP', 'USD', and 'Others'. I'm not sure if this is as complicated as the current issue here. Am I best creating a new issue to request support? or just add it here?

So far I've got new fields added to the index, one called 'ss_cck_price_currency', which takes its input from the Money CCK module. I would now like to add a filter which can be exposed to users allowing them to select from either 'GBP', 'USD', and 'Others'.

I think I need to code like this below, but I don't know what I'm missing? I've been updating this post as I've been trying more code out. Here's the latest....

Any advice?
cheers
scotjam

in my custom module...

	function extras_views_api() {
	return array('api' => '3.0-dev');}

	function extras_views_data_alter(&$data) {
		$data['apachesolr_node']['ss_cck_price_currency'] = array(
			'title' => t('Currency type'),
			'help' => t('Currency type. Drop down box with either USD, GBP or All Others'),
			'filter' => array(
			'handler' => 'apachesolr_views_handler_optionwidget_filter',
			),
			);
        }

in extras.views.inc...

<?php

/**
 * Implementation of hook_views_handlers() to register all of the basic handlers
 * views uses.
 */
function extras_views_handlers() {
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'extras'),
    ),
    'handlers' => array(
      'extras_views_handler_filter_currency' => array(
        'parent' => 'views_handler_filter_in_operator',
      ),
    ),
  );
}

in extras_views_handler_filter_currency.inc

<?php

class extras_views_handler_filter_currency extends views_handler_filter_in_operator {
  function get_value_options() {
      $options['USD'] = 'USD';
      $options['GBP'] = 'GBP';
      $options['Others'] = 'Others';
      $this->value_options = $options;
  }
  
  function operator_options() {
    return array(
      'in' => t('Is one of'),
    );
  }

  public function query() {
    if (empty($this->value) && ($this->options['exposed'] && empty($this->options['expose']['optional']))) {
      // Add term that will yield no results
      $this->query->add_filter('nid', apachesolr_views_query::escape_term('-1'));
    }
    else {
      $this->get_value_options();

      foreach ($this->value_options as $type => $name) {
        if (!in_array($type, $this->value)) {
          $this->query->add_filter($this->real_field, $type, TRUE);
        }
      }
    }
  }
}

Latest update... I've updated to code above since originally posting, and it appears now to work! Though it's all hardcoded.

Can someone kindly check if this looks right? and suggest improvements to avoid hard coding?

kenorb’s picture

+1

kenorb’s picture

mvc’s picture

related, but for core node date fields: #595610: Node Creation/Change date filter

kenorb’s picture

Issue summary: View changes
Status: Active » Closed (outdated)

Version 6.x is no longer supported due to Drupal 6 End of Life. For Drupal 8.x, use Search API Solr Searchinstead.