Introduction
Enables site builders to easily enable or disable fields and filters in a view conditionally based on the values of exposed/contextual filters or basically any request parameters.
Can be used for any display type and any format that uses the fields/filters defined in the view display.
Idea
The initial idea was to build more convenient content browsers where you show specific fields and filters only if the relevant content type was selected without having to programm custom code.
With this resulting module there are endless use cases to apply this. In example:
- You have a JSON REST export made with views and wanted to show specific fields or apply a specific filter only if specific query parameters and values are supplied.
- Or express even more complex "funnels" i.e: Apply non-exposed filter "a" only if exposed filter "b" is one of "foo" or "bar" and exposed filter "c" is greater than 25
- etc. ...
Configuration
General
When installing this module, a new section Dynamic fields and filters will be available at the advanced column on each display in the views edit-UI.
The Settings are per display, if you already have settings in another display of the same view you can copy them into the current display with a button in the dialog.
Configurate "parameters"
In the dialog you can assign up to 9 request parameters to listen to, so that you can enable or disable fields or filters when the value of a parameter matches a certain condition.
Enter the request parameter names as they appear in the query i.e "type" for view-url "example.com/my_view?type=foobar". Works for POST-parameters as well. You can also find the parameter names of exposed filters or exposed sorts in their edit-dialog under "filter identifier" or "sort field identifier". For Contextual filters it will only work if their "provide a default value type" is "Query parameter.
Condition syntax
Within the "administrative title" of a field or a filter you can use the condition syntax to only enable them if the parameter value matches the given expression:
dff{1..9}|expression|Administrative title
Where:
dff{1..9} refers to a parameter.
expression is either the string value to match or a more specific expression
Administrative title is your custom admin label, you can also leave this blank. But dont forget the trailing | on expression.
in example:
dff2|foobar| expresses: Parameter dff2 equals "foobar"
Always use the url-decoded version of the query value in expressions.
If in your url the parameter value is i.e foo+bar+foobar you would have to write:
dff2|foo bar foobar| to get a match.
Expression syntax
For more specific expressions you can use the expression syntax within a condition:
dff{1..9}|{operator:expression}|
Available operators are:
{neq:expression} - Not equals
{in:expression,expression2,expression3} - In array (delimiter is "," without a space)
{nin:expression,expression2,expression3} - Not in array
{gt:expression} - Greater than
{lt:expression} - Smaller than
{cn:expression} - Contains
{ncn:expression} - Not contains
If the query value itself is an array like in this example query:
?types[]=foo&types[]=bar(url decoded for readability)
Each element will be evaluated, the expression is true if one the elements matches.
Chaining syntax
You can logically chain multiple conditions with the chaining syntax:
dff{1..9}|expression|OPERATOR|dff{1..9}|expression|
Where:
OPERATOR is either AND, OR or XOR (uppercase)
You can use a total of 10 operators per label, each must be followed by the |dff{1..9}|expression| pattern.
In example:
dff2|{gt:5}|AND|dff4|{in:foo,bar}| expresses: dff2 is greater than 5 AND dff4 is one of "foo" or "bar"
For detailed information on how they work, check out the php´s Logical Operators The here provided operators work with the precedence like using "&&", "||" and "xor"
You can use the condition syntax for the parameters itself as well, this way you can make even more complex chaining without writing long and repeating conditions.
Installation
Install as you would normally install a Drupal module.
composer require 'drupal/views_dynamic_fields_and_filters:^1.2'
Requirements
This module works out of the box on a minimal Drupal install as it only requires "Views" which is required by system.
Drupal Core
Tested with core 8.9.x, 9.3.x, 9.4.x, 10.1 10.2.7 and php 8.1.0 & 7.4.26
Tested with core modules "Views REST export" & "Serialization" (They are not required to be enabled)
Tested with contrib modules "Views Conditional"
WYSIWYG
"What you see is what you get" The pictures below showcase a simple example where dff1 is the content-type filter and according to it´s value, specific fields and filters are shown like configurated in the administrative titles:
The main idea of using administrative titles for configuration is that you can directly see what dff does without any extra-clicks. The example configuration above would result in:
Based on the selected Content Type, specific fields and filters are enabled, but this is just the most simple example...
Feedback
Of any kind is highly approchiated, also if you try out this module and it doesn´t fulfill your requirements or does not work as you expected, feel free to open a feature request or an issue.
Depends on
Dependencies of the latest stable release
No dependencies recorded for this project.
Required by
Tracked projects that depend on this one
No tracked projects depend on this one yet.
More in the Views ecosystem
Most installed first