Unable to get the acf variable through controllers in a page called "Join us"

I have a page called “Join us” and I am having problems to get the acf values through controllers.
If I add @debug in my template file I can’t see the variables output.

this is my situation:

  • I created the job custom field

  • i have a page called “Join us” so I created the page-join-us.php inside /resources/views/

  • inside /resources/scripts/routes/ I created the join.js file and I updated the /resources/scripts/main.js (the js works well)

  • inside /app/controllers/ i created JoinUs.php with this code

namespace App\Controllers;
use Sober\Controller\Controller;
class JoinUs extends Controller
{
		protected $acf = true; // this will add all the ACF fields to the $data variable for this page
		
		public function job_test() {
			return get_field('job_test'); // this is a test to
		}

}

this is what I can see with the @debug in my “Join us” page, but as you can see there is not the JoinUs variable

what am I doing wrong?
any thoughts?

Have you tried PageJoinUs ?

The best way to debug would be to view the body classes for that page, does it match the Controller name? (bearing in mind Controller names are converted from camelCase to match the body class)

thanks @withjacoby unfortunately PageJoinUs doesn’t work, the body classes are
<body class="page-template-default page page-id-97 logged-in join-us app-data index-data singular-data page-data page-97-data page-join-us-data">

so looking at the body class “join-us” I wrote this in my controller class JoinUs extends Controller

In the FrontPage and WorkSingle (a custom post type page) works, for this reason I am confuse.

what else could I try?
any thoughts?

According the classes there, PageJoinUs should work. It’s the classes ending with -data that are used by Controller.

namespace App\Controllers;

use Sober\Controller\Controller;

class PageJoinUs extends Controller
{

}

Then rename the file to PageJoinUs.php for consistency.

Does that not work?

yeaaahhhh now works. thanks @withjacoby

just curiosity, why with the class JoinUs and JoinUs.php didn’t work?

is it always better and good practice to use the PageClassName instead of only ClassName (without page)?

thanks again

@mattia

Glad to hear!

It actually depends on the WordPress hierarchy. Controller uses https://github.com/Brain-WP/Hierarchy which adds classes depending on the template hierarchy. The body classes passed in by default from WP (ie. the ones without -data), are technically not following the default WP template hierarchy we use when creating templates/views. I made this decision when building Controller to try mimic Controllers to templates/views as closely as possible to provide a predictable structure.

Of course, it’s easy to get around this restriction if you need. Simply by using a filter to add a -data class, take a look here if that interests you, https://twitter.com/withjacoby/status/1026731012569661440

1 Like

This question has come up a few times, so adding the Controller names which could be used in a template/view would be a nice touch to @debug. I’ll get it added in for the next release.

Great, thanks for this and thanks again for you help :wink:

No problem! Good luck with the rest of the project.