MWDelaney / sage-acf-wp-blocks

Composer library for generating ACF Gutenberg blocks from templates. Intended for use with Roots/Sage (http://roots.io)
346 stars 66 forks source link

Issue passing data to a foreach loop in a gutenberg block #43

Closed fabianwurk closed 4 years ago

fabianwurk commented 4 years ago

I have a function set up in my App.php controller which I'm looking to pass to a Gutenberg block, but unfortunately its showing an error "Undefined variable: testimonial_loop in on line : invalid argument supplied for foreach() in on line" - as you will see on attached screenshot, I've added the same function and outputted through a non-Gutenberg blade template which works fine.Any idea why the block may be showing this error?

Screenshot 2020-06-28 at 21 46 23

public function testimonialLoop() { $testimonial_items = get_posts([ 'post_type' => 'testimonial', 'posts_per_page' => '-1' ]); return array_map(function ($post) { return [ 'title' => apply_filters('the_title', $post->post_title), 'content' => apply_filters('the_content', $post->post_content), 'thumbnail' => get_the_post_thumbnail($post->ID, 'large'), 'by' => get_field('testimonial_by', $post->ID), ]; }, $testimonial_items); }

which I'm trying to pass to a gutenberg block:

`{{-- Title: Testimonial Description: Customer testimonial Category: formatting Icon: admin-comments Keywords: testimonial quote Mode: edit Align: left PostTypes: page post SupportsAlign: left right SupportsMode: false SupportsMultiple: false EnqueueStyle: styles/style.css EnqueueScript: scripts/script.js EnqueueAssets: path/to/asset --}} @foreach($testimonial_loop as $item)

{!! $item['thumbnail'] !!}
<p>{!! $item['content'] !!}</p>
<p class="name"><strong>{!! $item['by'] !!}</strong></p>

@php wp_reset_postdata() @endphp @endforeach ` Thanks

MWDelaney commented 4 years ago

This is more of a question for the roots discourse.