Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

Conditional Fields in blueprint

Started by Richard Hainsworth 8 years ago · 4 replies · 1705 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 Ton Haarmans, 5 years ago
13 1140 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 63 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 138 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 114 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 132 7 months ago