Page init function not being called - requirements are not being loaded in head

SilverStripe 4.1

I am developing a search function using “Searchable Module” - GitHub - i-lateral/silverstripe-searchable: Adds to the default Silverstripe search by adding a custom results controller and allowing properly adding custom data objects and custom fields for searching

All of the templates are being pulled correctly from /app/templates/Layout/Page.ss. But, the Requirements setup in the PageController init function are not being loaded in the head when I visit the results pages generated by the module.

I tried adding the template files from the module to the app/templates folder, but that didn’t resolve the problem.

As always, any insight is greatly appreciated!

Can you post the Requirements calls which aren’t working?

protected function init()
    {
        parent::init();
			
				$assetsFolder = '/resources/themes/vintners/assets/';
			
				// CSS
				Requirements::css($assetsFolder.'lib/foundation/css/foundation.css');
				Requirements::css($assetsFolder.'lib/slick/slick.css');
				Requirements::css($assetsFolder.'lib/slick/slick-theme.css');
				Requirements::css($assetsFolder.'lib/foundation/css/app.css');
				Requirements::css($assetsFolder.'lib/font-awesome/css/fontawesome-all.css');
				Requirements::css($assetsFolder.'css/main.css');
				Requirements::css($assetsFolder.'css/wines.css');
				Requirements::css($assetsFolder.'css/pages.css');
				Requirements::css($assetsFolder.'css/news.css');
				
				// JS
				Requirements::javascript($assetsFolder.'lib/foundation/js/vendor/jquery.js');
				Requirements::javascript($assetsFolder.'lib/foundation/js/vendor/what-input.js');
				Requirements::javascript($assetsFolder.'lib/foundation/js/vendor/foundation.js');
				//Requirements::javascript($assetsFolder.'js/js.cookie.js');
				Requirements::javascript($assetsFolder.'lib/slick/slick.min.js');
				Requirements::javascript($assetsFolder.'lib/foundation/js/app.js');
				//Requirements::javascript($assetsFolder.'lib/isotope.pkgd.min.js');
				Requirements::javascript($assetsFolder.'js/main.js');
		}

I got this working, but unfortunately, it violates DRY.

I added this to the Searchable Controller class:

use SilverStripe\View\Requirements;

and then copied and pasted those requirement calls inside of its init() method.

I’d prefer to not repeat myself. Is it possible to access the PageController init() method from this (or any) module’s controller?

I’m slightly confused now, mostly by which files are where.

Could you just lay out the structure of what you’re doing? (ie. which controllers you’re talking about, whether any of the classes are extensions, etc.)

Is this the file you’re editing to make it work: silverstripe-searchable/SearchResults.php at 2 · i-lateral/silverstripe-searchable · GitHub ?

I notice that extends Controller rather than PageController which may be the root of the issue, since your requirements calls won’t be getting seen at all.

Edit: Having had a bit of a dig around, there is a similar question posed here: Usage with CMS · Issue #9 · i-lateral/silverstripe-searchable · GitHub

Sorry about that. Here are the two files in question from my solution:

/app/src/PageController.php is the main page controller class for my ‘app’. This contains the init() method with the Page requirement calls that were not working on “searchable” generated pages.

/vendor/silverstripe/searchable/src/control/SearchResults.php is the controller class for the module’s search results pages. I’ve imported “SilverStripe\View\Requirements” after the module’s other class imports and repeated the same requirements calls in this class’s init() method:

use SilverStripe\i18n\i18n;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\SSViewer;
use SilverStripe\View\ArrayData;
use SilverStripe\Control\Director;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\Control\Controller;
use SilverStripe\Subsites\Model\Subsite;
use ilateral\SilverStripe\Searchable\Searchable;
use SilverStripe\CMS\Controllers\ContentController;
// Added by me:
use SilverStripe\View\Requirements;

protected function init()
{
    parent::init();
    // Added by me: 
    Requirements::css($assetsFolder.'lib/foundation/css/foundation.css');
    Requirements::css($assetsFolder.'lib/slick/slick.css');
    //etc
}

That supports my thought about the module. The Searchable class is extending Controller so your requirements in the PageController aren’t being seen.

Have a look at the link I posted to issue #9 on the module’s page… it has a possible work-round.

1 Like

I’ll look into that solution. I have a couple of other ideas, too. I hope to solve it soon and I’ll share my solution. Thanks for the sleuthing!