Objective of this page is to explain how to use Numeric Field Filter module and what are the main benefits of doing so.

Numeric Field Filter is a module that extends the flexibility of Views usage. Namely it reveals much more advanced filtering on numeric columns of your entities (such as nodes, users, or any other). Standard Views implementation supports filtering values of numeric fields against constant factors. Numeric Field Filter substitutes such standard implementation introducing filtering against values of other numeric fields attached to entities.

Comparing a numeric field against another numeric field, not against a scalar factor

For example: you have a field "Distance to sea" and a field "Distance to the capital". Using this filter you can build up a view that will get you all the entities that are closer to sea than to its capital, i.e. it allows you to build queries like ...WHERE field_distance_sea > field_distance_capital. It supports all basic operators for number comparison like:

  • less than (<)
  • less than or equal to (<=)
  • equal (=)
  • not equal (!=)
  • greater than or equal to (>=)
  • greater than (>)
  • between
  • not between

Comparing a numeric field against an expression based off another numeric field

Apart from its ability to filter against values of another field, it also introduces ability of simple mathematical operations between fields before they are compared. Recalling to our previous example, a slightly more advanced version would be to get all entities that are closer to the sea at least by 1.5 times than to its capital. This kind of filtering is also possible with Numeric Field Filter module. Thus it would build a query like ...WHERE field_distance_sea > 1.5 * field_distance_capital. Right now filter allows you to execute the following operations on the fields before comparing them:

  • summation (+)
  • substraction (-)
  • multiplication (*)
  • division (/)
  • you may specify priority of calculation by using brackets (, )

Relationship Support

Functionality of this module wouldn't be complete, if it was not compatible with Views Relationships. In Views, Relationship is something that allows you to "join" a next table to the already existing tableset. Numeric Field Filter takes advantage of Relationships, which means you may join to one entity another, and than filter a numeric field from the first entity to a numeric field of another entity.

How to work with Numeric Field Filter

In this chapter you will be explained how to use Numeric Field Filter module. At this point you are expected to have installed Views, Field, and Numeric Field Filter modules. We will review the most feature-rich use-case, when all the features of this module are used. We will try to present this tutorial as a step by step easy-to-follow insturctions. Let's assume in our example that we have a node type 'car', that has the following properties:

  • date, when manufactured;
  • driver (a person who drives it) - entity reference to another node type.

Driver node type would only have the property of date, when he/she was born. Our task will be to build a view that filters all cars, whose drivers are at least 10-fold older than their cars.

  1. We will need a view, that works with cars.
  2. Then we would add relationship and join driver node type to our just created view.
  3. At this point we are ready to start filtering according to our logic. We should add a filter on the field "date, when manufactured". After adding this filter we will be asked to choose counterpart field. This is when the presence of Numeric Field Filter module gets exposed.
  4. Basically, the field "date, when manufactured" is going to be the left side of our comparison, now we have to make a desicion about the right side of the comparison expression. Among possible counterpart field options there is an option of a constant. This would result in standard behavior such as native numeric filter in Views. Other available options will be grouped by the base entities to which they belong. In our example we have 2 base entities:
    • entity of car;
    • entity of driver.
  5. We want to pick the field "date, when he/she was born" from the driver entity.
  6. Next we are supposed to select comparison operator. In our case it is "less than" (<).
  7. Lastly, after we have the left and right sides, and the operator of comparison, we get to the point, where we may enter formulas that will be given the value of each field and whose output will be compared. We have ability to enter formula as for the base field (left side of the expression), as for the counterpart field (right side of the expression). In our case we want to have age instead of year of manufacturing in the left side of expression. To achieve it, we have to subtract the current year (2013) from the year, when it was manufactured. Throughout our formula we may use the special placeholder [field] that will take the value of compared field at the moment of execution. This way our formula for the base field has to look like 2013 - [field].
  8. For the counterpart field (for the driver's age), we want to convert birth date into the age. After which, we still have to divide it by 10. This gives us the formula that is as simple as (2013 - [field]) / 10.
  9. At this point the creation of filter is finished. We may submit the form and observe the results of work. If your database contains such cars, whose drivers are older more than by 10 times than the cars themselves, you will see them in the resultset.

Using this simple example we have seen how Numeric Field Filter allows filtering value of one field against value of another field. In our case the counterpart field was attached to another entity (another node type) and before the actual comparison we have manipulated the values of both fields using simple and straightforward formulas. Nevertheless, quite complicated task has been achieved without writing a single line of code.


arcsump’s picture

Good day, it is a nice module, however is there a way to use a null vaue to compare values ( ie empty, missing, etc.)? Thanks!

bucefal91’s picture


Probably nope, Numeric Field filter can't do it. That's a great pleasure for me to know some one is interested in it. I really thought it was a dead module. Would you file an issue with this feature request?

This module has a great potential and I would like to bring it to a full release. With support from community expressed in testing and suggesting it will be much easier for me. Moreover I'll have this pressure "hey, people WANT it!" :)

http://takearest.ru - my Drupal baby :)

loudpixels’s picture

Hi there,

I’m trying to find a way to implement the following functionality on my website and was wondering if the module you’ve developed could help.

The website is about horse race betting and i’m trying to feature a virtual betting game where user can virtually try to play and guess the final results of a race.

So technically, I’m trying to compare the result of the race to the result of the users and determine which users has guessed the race in “order” and in “disorder”.”Order” would mean that the user has guessed the sequence of value in the correct order. “Disorder” would mean that the user has guessed the sequence of value but they’re not in the correct order.

Here’s a quick example:

The main race result would be: 12, 4, 7, 2, 8

User A guessed: 12, 4, 7, 2, 8 (order)
User B guessed: 4, 8, 7, 12, 2 (disorder)

At the end of each race I would like to be able to compute how many users won in “order” and “disorder”

Hope this makes sense.

Thank you,