Simple ACF if/else statement with Controller

Hi I have been struggling with how to recreate a standard ACF if/else statement using Controller.

EG I need to create this with a controller:

<?php if ( get_field( 'field_name' ) ): ?>

This is displayed when the field_name is TRUE or has a value.

<?php else: // field_name returned false ?>

This is displayed when the field is FALSE, NULL or the field does not exist.

<?php endif; // end of if field_name logic ?>

I have tried the following:

From my /app/controllers/App.php:

public function pageHeaderWidth() 
    {
        return get_field('page_header_width', 'options');
    }
    public function pageHeaderWidthOverride() 
    {
        return get_field('page_header_width_override');
    }

And in my view:

@if($pageHeaderWidthOverride( !== "") {!! App::pageHeaderWidthOverride() !!}
  @else {!! App::pageHeaderWidth() !!} @endif

However the only thing I can get to work is:

@if(get_field('page_header_width_override') !== "") {!! App::pageHeaderWidthOverride() !!}
  @else {!! App::pageHeaderWidth() !!} @endif

How can I repace that get_field with a variable from the controller. No matter what I try the $pageHeaderWidthOverride variable seems to have a value even though it does not output any characters or spaces. This means that I cannot use null, if_empty, or “” as ways to trigger the if statement. Any help would be greatly appreciated.

1 Like
@if($page_header_width_override)
   {!! $page_header_width_override !!}
@else
   {!! $page_header_width !!}
@endif

…Should work. Controller converts your camelCase method names to snake_case variables.

1 Like

Well the first thing I notice is that you’re calling these as static functions but haven’t declared them that way.

The way Controller works, you’ll get a variable for each non-static function (the kind you’ve declared in App.php) with the same name as its function name. You also don’t need brackets for your else statement in Blade. Also also CamelCase functions are converted to snake_case by Controller. So in the example you’ve given, you’d write this instead:

@if($page_header_width_override)
  {!! $page_header_width_override !!}
@else
  {!! page_header_width !!} 
@endif

You could simplify this further with a ternary:

{!! (isset($page_header_width_override)) ? $page_header_width_override : $page_header_width !!}

But even simpler would be to use Controller’s built in ACF functionality. This last method is my best recommendation.

3 Likes

That did the trick. I knew it must have been something simply. Clearly I missed the part about Controller converting camelCase to snake_case. Thank you so much. You just saved my forehead from repeated collisions with my desk.

1 Like

This is also very helpful. Still wrapping my head around Blade but so far so good (aside from the utter confusion, despair and general hopelessness I have felt over the last hour or so). Loving the way my templates look with the simpler markup.

You could simplify this even further with a null coalescing operator:

{!! $page_header_width_override ?? $page_header_width !!}

Although really I’d probably do it at the controller level:

// Controller file
public function pageHeaderWidth() 
{
   return get_field('page_header_width_override') ?? get_field('page_header_width', 'options');
}

// blade file
{!! $page_header_width !!}

Unless you need to know about which version is being used in the blade, this is a bit clearer to read.

4 Likes

Pretty sure my Controller knowledge just got a 1000% boost. Thank you so much for the help and clarification.

Can also be done a little cleaner with Sage Directives (shameless plug)

@hasfield('page_header_width_override')
  @field('page_header_width_override')
@else 
  @option('page_header_width')
@endfield
3 Likes

Just last week I was wondering why such a thing didn’t exist in PHP and then found out it did I just didn’t know I had to google “null coalescing operator” to find it. Silly me.

4 Likes

This topic was automatically closed after 42 days. New replies are no longer allowed.