This may well qualify as a Drupal cheap trick
. But it is pretty darn useful if you ask me.
The use case - I have a custom content type for daycares. In it, daycare owners can advertize the number of available spaces they have at any time. Creating a list of daycares showing available spaces is, of course, easy with Views. But what if I would like to show the total number of available spaces for the listed daycares. How would I do that?
At first, it's not obvious. But after the usual searching spree, I found the perfect solution. The good news is you can do it all with Views - don't need a special module for that
. By the way, my custom daycare content type contains various fields including an integer field for Available spaces
. Here we go.
- Create a new field-based view sorted by Unsorted (i.e. NO SORT) to which you'll add a block display (no need for a page display here).
- Add the Available spaces field. Remove all other fields.
- In the Advanced section, check Use aggregation. Save.
- Back in the FIELDS section, click Aggregation settings and set the type to SUM. Save. The field should now look like so: SUM(Content : Available spaces) | Aggregation settings.
- Click SUM(Content : Available spaces), click on Rewrite results and check Rewrite the output of this field using the available replacement tokens.
E.g. Total number of available spaces: [field_daycare_available_spaces]. where [field_daycare_available_spaces] is the token available for the field that holds the number of available spaces in my content type. Of course, this token will change based on the name of the field that holds the values that you want to add.
- Add the required filters.
- Save you view.
- Go to the blocks admin page and configure your block.
- Sit back and enjoy.
Got to love those cheap tricks...