ACF variables in blade template

blade
sage9

#21

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>

#22

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!


#23

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!


#24

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


#25

@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.


#26

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.


#27

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


#28

@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.


#29

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!


#30

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


#31

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

#32

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?


#33

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!


#34

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.