Page blueprint with conditional tab

Hello,

I try to set a condition to display a tab. But the tab under the condition is not displayed.
it’s properly displayed without the “conditional”.

title: Gallery
'@extends':
   type: default
   context: blueprints://pages

form:
 fields:
   tabs:
     type: tabs
     active: 1
     fields:
       gallery:
         type: tab
         title: PAGE_MAP.GALLERY.LABEL
         underline: true
         import@1:
           type: partials/gallery
           context: theme://blueprints  
       conditionfeat:
         type: conditional
         condition: "'true'"            
         fields:
           features:
             type: tab
             title: PAGE_MAP.FEATURES.LABEL    
             underline: true
             import@2:
               type: partials/features
               context: theme://blueprints 

I have tried the funny following code, which displays the fields of the second tab under the fields of the first tab :

title: Gallery
'@extends':
   type: default
   context: blueprints://pages

form:
 fields:
   tabs:
     type: tabs
     active: 1
     fields:
       gallery:
         type: tab
         title: PAGE_MAP.GALLERY.LABEL
         underline: true
         import@1:
           type: partials/gallery
           context: theme://blueprints  
   conditionfeat:
     type: conditional
     condition: "'true'"            
     fields:
       tabs2:
         type: tabs
         active: 1                  
         fields:
           features:
             type: tab
             title: PAGE_MAP.FEATURES.LABEL    
             underline: true
             import@2:
               type: partials/features
               context: theme://blueprints 

How can i handle this ?

Thanks for your help.

Did you try the usual condition: true (without quotes)? Or even condition: "true" or condition: 'true' (as docs say, it can be strings)? Why the double quotes? This should literally parse to 'true' (string with quotes in it and not just as string true)

Hi,

‘true’ or true don’t work.

“‘true’” works for a field.

                            type: conditional
                            condition: "'true'"            
                            fields:            
                                header.test:
                                    type: toggle
                                    label: TEST 

At the end, i will not use “‘true’” but a real condition like
condition: "config.plugins.myne.xxx ? 'true' : 'false'"

@unmick,

Is it possible?
From the docs:

The conditional field type is used to conditionally display some other fields base on a condition.

The term “fields” is quite ambiguous, but my first interpretation is “input fields” which excludes “tabs”.

The final verdict might be given by the Grav-team

Alternative ‘conditional’ approach:
You didn’t share the intention of using a ‘conditional tab’, so maybe the following alternative might not work in your use-case.

You could display a tab conditionally by conditionally publishing the overriding blueprint.

Usually a plugin publishes its blueprints in the event onPageGetBlueprints. E.g.:

public function onGetPageBlueprints(Event $event) {
   $types = $event->types;
   $types->scanBlueprints('plugin://' . $this->name . '/blueprints');
}

To publish the blueprint conditionally you could:

  • Subscribe to event onPageGetBlueprints only when condition is met.
    if ($condition) {
        $this->enable([
           'onGetPageBlueprints' => ['onGetPageBlueprints', 0],
        ]);
    }
    
  • Or you could wrap the code inside onPageGetBlueprints with an if-statement:
    public function onGetPageBlueprints(Event $event) {
       if ($condition) {
          $types = $event->types;
          $types->scanBlueprints('plugin://' . $this->name . '/blueprints');
       }
    }
    

IMHO, this approach is much cleaner and less error-prone than messing around in YAML form definitions.

Thanks! I will dig this solution.