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 (>)
- 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 (, )
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.
- We will need a view, that works with cars.
- Then we would add relationship and join driver node type to our just created view.
- 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.
- 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.
- We want to pick the field "date, when he/she was born" from the driver entity.
- Next we are supposed to select comparison operator. In our case it is "less than" (<).
- 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].
- 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.
- 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.