Conditional Logic

Conditional Logic

Quick Guide

In this example, we will create a field group with a true/false field and a text field. The text field will only be visible if the true/false field is set to true.

<?php
 
use JorenRothman\ACFBuilder\FieldGroup;
use JorenRothman\ACFBuilder\Fields\Choice\TrueFalse;
use JorenRothman\ACFBuilder\Fields\Basic\Text;
use JorenRothman\ACFBuilder\FieldConditionalLogic;
 
$fieldGroup = new FieldGroup('My Field Group');
 
$trueFalse = TrueFalse::make('My True False Field');
 
$isTrueFalseTrue = new FieldConditionalLogic();
$isTrueFalseTrue->and($trueFalse, '==', true);
 
$textField = Text::make('My Text Field');
$textField->setConditionalLogic($isTrueFalseTrue);
 
$fieldGroup->addField($trueFalse);
$fieldGroup->addField($textField);

Chaining with or()

Use or() to add an alternative condition group. The field will be visible when any group matches.

$logic = new FieldConditionalLogic();
$logic
    ->and($statusField, '==', 'published')
    ->or($roleField, '==', 'editor');

Operators

OperatorDescription
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
containsContains value
not_containsDoes not contain value
patternMatches pattern