soberwp/controller

Data Fields Don't Inherit From Hierarchy?

natefoundry opened this issue · 8 comments

I created a new page called Home (page-home.php) and set it as my Front Page. I'm attempting to use the ACF plugin to add fields to the home page and created a controller for it:

namespace App;

use Sober\Controller\Controller;

class PageHome extends Controller
{
    public function hero_video()
    {
        return get_field('hero_video');
    }
}

In my page-home.blade.php view:

  <div class="hero-background">
    <video autoplay muted loop playsinline>
      <source src="{{$hero_video}}" type="video/webm"/>
    </video>
  </div>

However, it doesn't seem to exist when I debug it:

Controller Debugger:
$site_name » string
$post » object
Hierarchy Debugger:
controllers/app.php
controllers/index.php
controllers/singular.php
controllers/page.php
controllers/page-14.php
controllers/page-home.php
controllers/front-page.php

If I move the hero_video() method to the controller for front-page.php it works:

<?php
namespace App;

use Sober\Controller\Controller;

class FrontPage extends Controller
{
  public function hero_video()
  {
      return get_field('hero_video');
  }
}

Debug output after change:

Controller Debugger:
$site_name » string
$post » object
$hero_video » string
Hierarchy Debugger:
controllers/app.php
controllers/index.php
controllers/singular.php
controllers/page.php
controllers/page-14.php
controllers/page-home.php
controllers/front-page.php

Is this the way inheritance for data fields is supposed to work? If not, what am I doing wrong?

Hi @natefoundry, would you mind updating to 2.0.1 and then trying again? You will need to update namespaces (from App to App\Controllers), and rename folder controllers to Controllers/

Here's a PR that shows the update process clearly

roots/sage#2025

I updated my composer file to use ~2.0.1, then renamed the directory and namespace for each controller, but now I have this error:

Fatal error: Uncaught ReflectionException: Class App\Controllers\page-home does not exist in /Users/nathan.perrier/git_repos/h4h/homesforheroes/web/app/themes/h4h-theme/vendor/soberwp/controller/src/Loader.php:120 

Stack trace: #0 /Users/nathan.perrier/git_repos/h4h/homesforheroes/web/app/themes/h4h-theme/vendor/soberwp/controller/src/Loader.php(120): ReflectionClass->__construct('App\\Controllers...') 
#1 /Users/nathan.perrier/git_repos/h4h/homesforheroes/web/app/themes/h4h-theme/vendor/soberwp/controller/src/Loader.php(49): Sober\Controller\Loader->setClassesAlias() 
#2 /Users/nathan.perrier/git_repos/h4h/homesforheroes/web/app/themes/h4h-theme/vendor/soberwp/controller/controller.php(22): Sober\Controller\Loader->__construct(Object(Brain\Hierarchy\Hierarchy)) 
#3 /Users/nathan.perrier/git_repos/h4h/homesforheroes/web/wp/wp-includes/class-wp-hook.php(286): Sober\Controller\loader('') 
#4 /Users/nathan.perrier/git_repos/h4h/homesforheroes/web/wp/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array) 
#5 /Users in /Users/nathan.perrier/git_repos/h4h/homesforheroes/web/app/themes/h4h-theme/vendor/soberwp/controller/src/Loader.php on line 120

It needs to be PageHome.php to follow PSR4.

That fixed it. Same for all the other controllers too (including app.php)?:

★  Controllers (master u=) $ ls -1
FrontPage.php
PageHome.php
App.php

Well, all that did was update me from 9.0.0 beta to 2.0.1. I still don't see the data fields on my page-home blade template.

For the hierarchy, are you using class FrontPage extends Controller implements Tree, or using protected $tree = true; in FrontPage.php? You would need to do that to inherit from PageHome.php

That did it. Thanks for the help.