Roots Discourse

Controller for ACF block

I’m using mwdelaney/sage-acf-gutenberg-blocks to create my custom ACF Gutenberg blocks. In this case, I have a custom Gutenberg block that is included on the front page.

In the markup of my block I would like to access some logic that is defined in a Sage controller. But neither the variables declared in the app controller nor the ones in the home controller are accessible.

// in app/Controllers/App.php
public function test()
{
    return 'hello world from App controller';
}
// in app/Controllers/FrontPage.php
public function test2()
{
    return 'hello world from FrontPage controller';
}

I would expect this to work:

// in my ACF Custom Gutenberg Block code
{{ $test }}
{{ $test2 }}

But instead I’m getting undefined variable errors.

Is there a way to make this work? Other than moving my logic to the block code?

1 Like

+1 for me as well. I was trying to make a ‘news’ block that pulls in 3 posts and had a function in the app controller that did all the args and what not so that the template had a simple foreach loop. Can’t get it to pass into the block.

Within the context of this package, I don’t believe you can do this. If you look at the code that renders blocks, it’s not being executed in the context where Controllers are available. If does have a filter you could use to pass data, and potentially you could wire that up to Controller somehow, but as far as I can tell it won’t support it “out of the box.” There’s a bit of a workaround in the issues: https://github.com/MWDelaney/sage-acf-wp-blocks/issues/5