The ConditionalLogic class enables you to show or hide fields based on the values of other fields without needing to know their specific ACF field keys.
Available Operators
==, !=, >, <, ==pattern, ==contains, ==empty, !=empty.
Logic Patterns
- AND condition: Chain
.and() methods. - OR condition: Pass multiple
ConditionalLogic::where() calls within the conditionalLogic() array. - Cross-group logic: Use the
group parameter in where() to reference a field from a different field group.
Example
Select::make('Type')
->choices(['document' => 'Document', 'link' => 'Link', 'embed' => 'Embed']),
File::make('Document', 'file')
->conditionalLogic([
ConditionalLogic::where('type', '==', 'document')
]),
// OR condition using multiple entries in the array
Text::make('Title')
->conditionalLogic([
ConditionalLogic::where('type', '!=', 'document'),
ConditionalLogic::where('type', '!=', 'link')
]),
// Cross-group condition
Text::make('Sub Title')
->conditionalLogic([
ConditionalLogic::where(
group: 'other-group',
name: 'enable_highlight',
operator: '==',
value: 'on',
)
]),
use Extended\ACF\ConditionalLogic;
use Extended\ACF\Fields\File;
use Extended\ACF\Fields\Select;
use Extended\ACF\Fields\URL;
use Extended\ACF\Fields\Textarea;
use Extended\ACF\Fields\Text;
Select::make('Type')
->choices([
'document' => 'Document',
'link' => 'Link to resource',
'embed' => 'Embed',
]),
File::make('Document', 'file')
->conditionalLogic([
ConditionalLogic::where('type', '==', 'document')
]),
URL::make('Link', 'url')
->conditionalLogic([
ConditionalLogic::where('type', '==', 'link')
]),
Textarea::make('Embed Code')
->conditionalLogic([
ConditionalLogic::where('type', '!=', 'document')->and('type', '!=', 'link')
]),
Text::make('Title')
->conditionalLogic([
ConditionalLogic::where('type', '!=', 'document'),
ConditionalLogic::where('type', '!=', 'link')
]),
Text::make('Sub Title')
->conditionalLogic([
ConditionalLogic::where(
group: 'other-group',
name: 'enable_highlight',
operator: '==',
value: 'on',
)
]);