7

I'm looking for a solution that would allow me to use tokens in views filter values. For example, if I have text field 'my_custom_text' I would like to have a filter that would allow me to use 'Is equal to' operator and a token value instead of hardcoded string.

Note that I can't use contextual filters because I have few filters that needs to be ORed.

Is there a module for that, or do I have to code that myself from ground up?

4 Answers 4

3

Use Contextual Filters instead of Filter Criteria.

In Drupal 8, at the time of writing this, you also have to install the views_argument_token module.

When adding a contextual filter, at the pop up window, select "Provide default value" option. From there on the remaining settings should be intuitive.

0

You can use hook_views_query_alter to done your job,

implement this hook in your custom module and with if statement restrict to your views name and edit views query,

7
  • That I'm aware of. I was rather looking for some robust existing solution. It's weird that it isn't possible to use tokens or at least PHP code to obtain filter value. After all views is one of the most popular modules. Nov 22, 2013 at 18:33
  • yes, you can also use cutom_php module and add php GLOBAL filter , and provide what you want
    – Yuseferi
    Nov 22, 2013 at 19:26
  • I don't want to use GLOBAL filter. I only want to provide custom PHP (or token) for the string filter value. This is for existing field. Nov 22, 2013 at 20:52
  • I know, bu using global php filter you can add criteria on other field, I used this method sometimes and worked
    – Yuseferi
    Nov 23, 2013 at 4:30
  • unfortunately 'global' filter is kind a hack, not a real solution. It is used to filter data that is already loaded from the DB. This is useless if the returned result set is huge. And this is my case - the filter I want to implement will significantly limit number of rows returned, probably from approximately 1000 to just few. It would be really unwise to load 1000 rows from DB just to display only few of them :) Nov 29, 2013 at 12:27
0

So here is what I've came up with eventually.

  1. I did not want to use global php filter because it only limits the results that are already loaded from the DB. In my case this would mean loading 1000 rows just to display 2 of them. Not too wise I'd say.
  2. I did not want to use hook_views_query_alter as this would mean that I'd have to dig into the code every time someone wants to change or create a view.

I ended up writing my own views handler that extends one of the exiting. I've also used hook_field_views_data_alter to hook up my handler instead of default.

This solution works quite nice. Custom handler is very robust solution and quite simple to implement... once you know how to do it ;)

-1

Would the Token Filter module be helpful? Here is a quote about it (from the module's project page):

... is a very simple module to make token values available as an input filter.

The module now supports native tokens (e.g. [site:name]) and all global tokens by default. It even allows some contextual tokens like node or user to be replaced, but currently does not encourage users to use them in the filter help text.

2
  • Welcome to Drupal Answers! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – mpdonadio
    Dec 21, 2013 at 21:22
  • tokens cannot be used in views by design
    – Yuri
    Sep 6, 2017 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.