ACF variables in blade template

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

Is there a way to include partials in the controller while using flexible content field, not just single field data?

What I’m trying to do is to have the usual acf check for the flexible content field in the controller, get the corresponding partial and then display the whole thing in the view file, while each partial can show its own sub fields.

Basically I’m trying to make a simple page layout builder in the back-end and I use the flexible content field for that. There would be these pre-made page sections like a page-banner, or a basic text content with title or a gallery, etc. And I want to make all these as individual partials. Just can’t really figure out how to make the controller spit it out on the page.

Not sure if I tackled the problem from the right direction, I just started learning the new sage and blade templating in general a few hours ago. I tried to avoid doing it with my old way which is having the acf if/while checks then including the specific partial in the page template file.

Any examples and help are greatly appreciated.

I am also doing the same thing. I have gotten to the point where I can display different content per ACF shelf but it does not utilize the controller.

i did have a solution where i could use multiple ACF controllers on any page; however, when i wanted to reuse a flexible content shelf on the same page, it would return the value of the first content row.

Example:
Home Page : Flexible Content : Row 1 - Content, Row 2 - Image, Row 3 - Content. The values of the variables from the content controller - Row 1 and Row 3 - are the same on the front end, but on the back end they are different.

I ended up putting together something based on nathobson’s tips in the controller, but let the partial handling in the view file. That wasn’t my original goal but I thought that much logic is okay(or necessary) since it is the same how it is in the default pre-made files.

controller:

public function getLayouts()
  	{

      $layouts = get_field('layout');
      $data = [];
      if($layouts):
        foreach($layouts as $layout):

          // common layout attribute settings
          $this_layout = (object)[
            'background_color' => $layout['settings']['background_color'],
            'background_image' => $layout['settings']['background_image'],
            'custom_class' => $layout['settings']['custom_class']
          ];
          // specific attributes based on layout type
          if($layout['acf_fc_layout'] == 'large_carousel'):
            $this_layout->type = 'large_carousel';
            $this_layout->slides = $layout['carousel_slides'];
          elseif($layout['acf_fc_layout'] == 'text_section'):
            $this_layout->type = 'text_section';
            $this_layout->full_width = $layout['settings']['full_width'];
          endif;

          array_push($data, $this_layout);

        endforeach;
        return $data;
      endif;
  	}

View:

@foreach($get_layouts as $layout)
      @if($layout -> type == 'large_carousel')
        @include('partials.page-section-carousel')
      @elseif ($layout -> type == 'text_section')
        @include('partials.page-section-text')
      @endif
    @endforeach

Inside a partial a you can get the values you set up for the object with $layout -> somestuffyouneed.

It isn’t much more efficient than the regular acf while loop but at least the flexible content data is available in the controller now. Not sure if it helps in your case but maybe you can get some ideas and improve my mess.

edit: Oh and I used this for debug purposes in the loop to check how ACF made the structure:

  echo '<pre>';
  print_r($layout);
  echo '</pre>';
	      die;

How are you reusing the same template on the same page?

it’s there in the code examples really. In the controller I build an array of layout objects from the acf input and in the view it goes through the array and displays the corresponding layout template. It doesn’t matter how many times you put in the same template in the back end because they become different objects in the controller.

There are some stuff there that is specific to my case but the main part is the $this_layout which is current object that will be put into the $data array.

maybe this way it’s clearer:

controller:

public function getLayouts()
{
	//acf flexible content field
	$layouts = get_field('layout');
	//this will hold all the layouts
	$data = [];
	//go through each layouts that got put in there
	if($layouts):
		foreach($layouts as $layout):

			//$this_layout is the current one
			$this_layout = (object)[
				//the layout object's title value will be the 'title' named acf sub field of the flexible content layout
				$this_layout->title = $layout['title'];
			];

			// if you want to add specific fields based on flexible content layout type, you can check it this way
			if($layout['acf_fc_layout'] == 'text_section'):
				//the layout object's type value is going to be 'text section'
				$this_layout->type = 'text_section';
			endif;

			//put the current object into the $data array
			array_push($data, $this_layout);

		endforeach;
		//return the $data array with all the flexible content objects in it
		return $data;
	endif;
}

view file

@extends('layouts.custom')

@section('content')
  @while(have_posts()) @php the_post() @endphp

		//get_layouts function from the controller and run through each
    @foreach($get_layouts as $layout)
			//if the object's type value is 'text_section' then do this
      @if ($layout -> type == 'text_section')

        <section class="page-section standard-text-section">
					<div class="container">
						<div class="text-wrapper">
              //display one of the sub field values we set up in the controller this way
							<h1>{{$layout -> title}}</h1>
              <p>{{$layout -> type}}</p>
						</div>
					</div>
				</section>

      @endif
    @endforeach

  @endwhile
@endsection

Hi Jacoby, love your work here. Thanks for all your help.

Just wondering how this ACF module works using get_option('page_for_posts')

As an example I’m trying to get a custom field on a blog page, but when I @debug using protected $acf = ['field1']; returns no data.

1 Like

The new version releasing soon will have support for acf option page. You would just just use $acf = true; in App.php

2 Likes

Appreciate it mate… Don’t mean to pester but any idea when the new version may released?

In the meantime should I just forgo using the ACF module and use the original method?

1 Like

I’ll tag it this afternoon.

First of all, thanks for this post as we’re able to use this to have clean templates!

If one were trying to return the default WP thumbnail cropped image size from an ACF image field in a repeater field, what’s the best way to do that? Also, how to correctly grab the alt tag for the image?

Controller:

public function models()
{
    return array_map(function($model) {
        return [
            'title' => $model['model_type_name'] ?? null,
            'content' => $model['short_description'] ?? null,
            'image' => $model['image'],
            'aluminum' => $model['aluminum_colors'] ?? null,
            'poly' => $model['poly_roof_colors'] ?? null,
            'heat' => $model['heat_poly_roof_colors'] ?? null,
            'wind' => $model['wind_pressure'] ?? null,
            'snow' => $model['snow_load'] ?? null,
        ];
    }, get_field('model_series') ?? []);
}

Loop:

@foreach($models as $model)

<div @include('partials.content-single-our_models_list', $model) </div>

@endforeach

</div>

File:

<h3>{ $title }}</h3>

<img src="{{ $image }}" alt="{{ $image ['alt'] }}">

Lastly, how would you go about returning the values of a set of checkboxes if they’re checked?

@if($aluminum['slc'])
    @php echo "sun silver"; @endphp
@else
    @php echo "n/a"; @endphp
@endif