Roots Discourse

I can var_dump the ACF array, but I am unable to display it and how can I loop through to display everything

In my controller, I have the following simple method, which when I do a var_dump, shows me all the ACF elements within the array

class TemplateMission extends Controller

{
protected $acf = true;
public $test;

public function mission() {
    if (have_rows('all-flex-fields-mission')):

        while(have_rows('all-flex-fields-mission')): the_row();

            $left_hand_content = get_sub_field('left-hand-content');
            var_dump($left_hand_content);

          endwhile;
        endif;
}

use Partials\FlexibleLayout;
}

The array that is dumped looks like the following:

        array (size=1)
    0 => 
        array (size=5)
        'acf_fc_layout' => string 'content' (length=7)
        'content-icon' => 
            array (size=24)
            'ID' => int 10846
            'id' => int 10846
            'title' => string 'champion' (length=8)
            'filename' => string 'champion.png' (length=12)
            'filesize' => int 10666
            'url' => string 'http://dev.xyz.local/wp-content/uploads/2020/08/champion.png' (length=60)
            'link' => string 'http://dev.xyz.local/mission-test/champion/' (length=43)
            'alt' => string '' (length=0)
            'author' => string '10' (length=2)
            'description' => string '' (length=0)
            'caption' => string '' (length=0)
            'name' => string 'champion' (length=8)
            'status' => string 'inherit' (length=7)
            'uploaded_to' => int 10828
            'date' => string '2020-08-07 16:26:16' (length=19)
            'modified' => string '2020-08-07 16:26:16' (length=19)
            'menu_order' => int 0
            'mime_type' => string 'image/png' (length=9)
            'type' => string 'image' (length=5)
            'subtype' => string 'png' (length=3)
            'icon' => string 'http://dev.xyz.local/wp-includes/images/media/default.png' (length=57)
            'width' => int 41
            'height' => int 48
            'sizes' => 
                array (size=18)
                ...
        'content-title' => string 'CHAMPION FOR CONSUMERS' (length=22)
        'content-subtitle' => string 'Developing a Better Way to Shop for' (length=45)
        'wysiwyg' => string '<p>We want to make finding the best broadband as easy as possible, so we build tools to help consumers do just that.</p>
    <p>But, that’s just the start.</p>
    <p>At our core we are champions for the consumer.</p>
    <p>To us this means:</p>
    <ul>
    <li>Bringing transparency in government data and research.</li>
    <li>Aggregating IP verified customer reviews and ratings.</li>
    <li>Fostering competition through better awareness of local providers.</li>
    </ul>
    ' 

Which matches what I have in the Pages

In my template page, I have the following:

@while(have_rows('left-hand-content')) @php(the_row())

@php($content_icon= get_sub_field('content-icon'))
@php
  echo 'makes it here';
@endphp
@if($content_icon)
  <div>
    {!! $content_icon !!}
  </div>
@endif

@endwhile

How is it possible that I have access to the entire array, but I cannot display a single item?

I have also tried the following to just loop through the get_row_layout:

@if(TemplateMission::mission())
  @while(have_rows('left-hand-content')) @php(the_row())
  @php echo 'gets here'; @endphp
  @php $fieldname = get_row_layout()  @endphp
    @php echo $fieldname; @endphp
  @endwhile
@endif

I have tried so many things. On one the iterations the template would echo the word “basic” - which is the name of the layout. I was hoping to do something like the following with fieldname:

@include('flexible.'.$fieldname)

which would then call a file within the directory “flexible”
I was hoping to see the words “image” or “text” or “wysiwyg”. From there, I have file “wysiwyg.blade.php,” which contains the following simple line of code:

{!! get_sub_field(wysiwyg) !!}

Within the “flexible” directory, I also tried creating a “basic.blade.php,” which contains the following simple line of code:

{!! get_sub_field(wysiwyg) !!}

Now, I cannot even figure out how I had the word “basic” echo. Now the page is just blank, unless I dump the array in the controller and I can see the entire array. Please help, I have been trying this since this morning

In most cases I feel like using ACF’s special functions like get_sub_field are more trouble than they’re worth–especially when you can just get your repeater or flexible content field as an array and then handle that data yourself.

I’m not entirely sure what you’re trying to accomplish with the code you’ve posted. Your Controller method returns no data, so your templates would have no access to anything the Controller does. You also don’t try and access that controller data (which, based on your method name, would be available in the $mission variable), so I’m not sure what you’re trying to do with it. It looks like maybe you’re trying to set up some kind of context in the Controller, but that won’t work: Blades aren’t executed in the context of Controllers, they just receive data from them.

My understanding of your situation is:

  • Your have a Flexible Content Field (called, I think, all-flex-fields-mission)
  • You want to loop over the rows in this field and conditionally include partials based on the type of field

If that is indeed the case, I would suggest something like this;

// TemplateMission.php
class TemplateMission extends Controller {
  public function mission() {
    /** 
      * I like to specify where the field is getting its value from, even if it's
      * like this and I'm just replicated the internal functionality, because it
      * it helps me to debug later.
      */
    return get_field('all-flex-fields-mission', get_the_ID());
  }
}
<!-- views/whatever-template.blade.php -->
@foreach($mission as $row)
  <!-- `acf_fc_layout` is where each flexible content field stores 
          the name of that layout. -->
  @include('partials.' . $row['acf_fc_layout'])
@endforeach
<!-- views/partials/content.blade.php -->
<!-- This would be called for the flexible content field called 
       "content" -->
<div>
   <h2>{{ $row['content-title'] }}</h2>
   <span>{{ $row['content-subtitle'] }}</span>
   <!-- etc... -->
</div>
1 Like

Thank you so much, you have me moving in the right direction, with your answer. Everything is returning in the loop, except for the wysiwyg is returning as plain text. Any thoughts on how I can return it as html

@foreach($mission as $key => $row)

              <div class="media-left">
              <span class="media-object media-object-bordered media-object-bordered-danger-lightest">
                <img src="{{ $row['left-hand-content'][$key]['content-icon']['url'] }}" />
              </span>
              </div>
              <div class="media-body p-t-6">
                <h2>{{ $row['left-hand-content'][$key]['content-title'] }}</h2>
                <span class="sub-headings">{{ $row['left-hand-content'][$key]['content-subtitle'] }}</span>
              </div>
              <div>
                {{ $row['left-hand-content'][$key]['wysiwyg'] }}
              </div>

@endforeach

image

In order to display un-escaped data you need to use {!! !!}, i.e.

{!! $row['left-hand-content'][$key]['wysiwyg'] !!}

I strongly recommend reading through the Blade documentation where that and many other very useful subjects are discussed.

Thank you sir - I do not know if I should select this as the answer or your previous reply, but either way, you have helped me tremendously.

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