ACF variables in blade template

Just did more work on automating the acf fields from Controller to view.

The returned variables are based on acfs key values. You can then override any values by using the same key value in camel case in your Controller for more control.

<?php

namespace App\Controllers;

use Sober\Controller\Controller;

class Single extends Controller
{
    protected $acf = true;

    public function textField()
    {
        return 'Override automated field';
    }
}

@debug

$site_name » string
$post » object
$text_field » string
$repeater_field » array[2]

Multilevel dimensional arrays are returned in object notation based on your acf field keys.

Repeater field example below;

<h1>Repeater Field</h1>
<ul>
  @foreach($repeater_field as $item)
    <li>{{ $item->repeater_field_item }}</li>
  @endforeach
</ul>
8 Likes

Hey all,

I’m trying to get ACF fields working and an encountering some issues.

I’m running the most recent Sage beta, and I can not figure out how to get the fields exposed as simple variables as I see everyone doing here.

Note that my image field is just returning the URL string.

Controller app/controllers/App.php:

namespace App;

use Sober\Controller\Controller;

class App extends Controller
{
    public function heroImg()
    {
        $image = get_field('hero_image');
        return $image;
    }
}

working views/partials/content-page.blade.php

@if (App::heroImg())
  <div class="bg-img" style="background-image: url('{!! App::heroImg() !!}')">
@endif

not working:

@if ($heroImg)
  <div class="bg-img" style="background-image: url('{{ $heroImg }}')">
@endif

I feel like the latter should be working based on what I see in this thread. Did I miss a step? Thank you!

I just figured this out, I missed an important bit in https://github.com/soberwp/controller/blob/master/README.md

Important: The method name is converted to snake case and becomes the variable name in the Blade view.

heroImg() becomes $hero_img and now all works fine.

Hope this helps anyone else with the same issue!

5 Likes

This looks nice! Would you mind sharing the Acf class too?

@threehz Been there, done that. It is one of the most confusing things about Controller.

I wished there was a simpler way of defining data without going through a separate function for each variable.

1 Like

Did you read the whole thread? You absolutely don’t need a separate function for each variable. Just build an array/object of your data to return within your method.

1 Like

This is clever. I feel like this is how I will do it moving forward.

1 Like

@nathobson

Did you read the whole thread? You absolutely don’t need a separate function for each variable. Just build an array/object of your data to return within your method.

Yes. My comment “been there, done that” was about snake casing of the method names BTW.

Whether you package all the variables into an array, object, or even a collection, wasn’t my point. On the Blade side, you have to do $object->related and $object->unrelated kind of thing or define separate functions for the unrelated.

Either way, when does it stop being a controller and actually be a transformer, or both. Just confusing, is all I am saying.

Similar to @olafghanizadeh’s post, I am trying to display the ACF Post Object on front-page.blade.php. I first tried to display using vanilla php (using ACF’s Post Object article) before attempting to use proper blade and controller syntax just to see if it works:

@extends('layouts.app')

@section('content')

  <?php $post_object = get_field('featured_article'); ?>
  <?php if($post_object): ?>
    <?php $post = $post_object; ?>
    <?php setup_postdata($post); ?>
    <div>
      <h3><a href="{{ the_permalink() }}">{{ the_title() }}</a></h3>
    </div>
  <?php wp_reset_postdata(); ?>
  <?php endif; ?>

@endsection

However, it is displaying the link and title of the Home page and not the selected Post in ACF editor:

I also, viewed the data using print_r and it is properly grabbing the selected Post data. Thanks in advance for anyone’s help!

Try the “Basic loop (without setup_postdata)” method from the ACF relationship documentation.

1 Like

The basic loop method is also displaying the link and title of the Home page. Here is the code I used from the documentation:

@extends('layouts.app')

@section('content')

<?php $posts = get_field('featured_article'); ?>
<?php if( $posts ): ?>
  <ul>
  <?php foreach( $posts as $p ): ?>
      <li>
        <a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( $p->ID ); ?></a>
      </li>
  <?php endforeach; ?>
  </ul>
<?php endif; ?>

@endsection

I’m doing exactly what you’re doing in several themes (I just double-checked my code) and it’s working. Are you sure your field is named featured_article ?

If you add print_r($p) right after your foreach does it give you info about the featured article?

1 Like

I got it to work with the last code block I posted using the Relationship ACF Field Type and your tip to use the Basic loop method. However, the Post Object keeps displaying the Home page. That’s ok though, I can use the Relationship Field Type for the purpose of this page and I got it to work with controller/blade syntax as well. Thanks so much for the help!

I’ll be sharing the Acf module for Controller with the next release which should be soon.

The automation will speed up a lot of this repetitive work for each field.

4 Likes

The documentation says: "Controller has an useful Advanced Custom Fields helper module to automate passing on fields.

The automated fields will use the variable names from Advanced Custom Fields and pass them onto the view."

How, can i call it from the view (i am using in my category archive)?

I cant see nothing when i use the @debug

I would recommend raising an issue on the GitHub repo and I’ll take a look for you.

1 Like

Hi there, do you know when Controller 2.0.0 is going to be used by Sage as a default?

Are there any potential issues with updating this manually in order to get the most out of the ACF module you’ve so brilliantly created? As suggested in this thread: Setting up a controller for ACF fields

How will this be affected by future updates to Sage?

Cheers,
J

You can just bump the version in package.json. Here’s the files that need to change with the upgrade to 2.0.x

I see lot of comments about using/accessing variables with get_field.

How about get_sub_field inside of a flexible field group?

Easy way would be to use get_field and store the flexible field data into a variable and then foreach over it to access the data you need (in your controller).