Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Conditional Fields in blueprint

Started by Richard Hainsworth 8 years ago · 4 replies · 2076 views
8 years ago

Can't seem to get a conditional field to work in a Blueprint when a select option is used.
In the following it is the field 'condition' under 'thirdparty' that is causing me pain.
(This is a part of a plugin's blueprints.yaml file.)

YAML
        provider:
            type: select
            label: Map imagery provider
            options:
                openstreetmap: Open Street Map
                thunderforest: Thunderforest
                mapbox: Mapbox
            default: openstreetmap
        thirdparty:
            type: conditional
            condition: "config.plugins.map-leaflet.provider == 'openstreetmap' ? 'true' : 'false' "
            fields:
                apikey:
                    type: text
                    label: API Key if provider needs it
                    help: You should register your own API key with the provider and include it here
                    default:  'querty'

I have tried is same as instead of == in condition, but I still get problems.

I save the change each time. I have got conditional fields working before in blueprints, but only with fields that themselves were boolean.

I can't figure out what I am doing wrong.

8 years ago

@finanalyst I'm just guessing that the condition is parsed and executed by Javascript. The dash in 'map-leaflet' might cause problems.

What if you try:

TXT
"config.plugins['map-leaflet'].provider == 'openstreetmap' ? 'true' : 'false' "

Again, just guessing....

8 years ago

The GRAV documentation says the string is processed with twig. When I made an error in the string, I got an error message from the Twig processor.

I'll try your workaround.

Further: The following worked:

TXT
"config.plugins['map-leaflet'].provider == 'openstreetmap' "

WIth the ? 'true' : 'false' part, it did not.

Thanks

last edited 09/10/18 by Richard Hainsworth
8 years ago

Glad it's working...

Twig variables:
Also Twig does not handle variables containing a dash.

When the attribute contains special characters (like - that would be interpreted as the minus operator) [...]

Twig truthy-ness:

The rules to determine if an expression is true or false are the same as in PHP;

Which means that both strings 'false' and 'true' returned by your condition are true. Does that align with your experience?

I wonder if the example in Grav's docs about the Conditional Field using the quoted false and true is correct...

8 years ago

About 'true' and 'false', I think you are right, and it certainly is what I found.

Thanks for the quick response and the '-' tip. I know there is a problem with '-', but I haven't yet worked out quite where. Sometimes I can use is, sometimes not. Your point about Twig and '-' is helpful.

That would indicate the GRAV documentation is not correct, unless I am missing something.

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Anna, 2 days ago
2 53 16 hours ago
Forms & Blueprints · by Anna, 3 days ago
7 82 16 hours ago
Forms & Blueprints · by Anna, 1 week ago
5 186 6 days ago
Forms & Blueprints · by TomW, 1 month ago
3 379 1 month ago
Forms & Blueprints · by Japhy, 2 months ago
2 595 2 months ago