Martin Muzatko Posted September 13, 2017 Share Posted September 13, 2017 Hello! I started to implement this tutorial today. I am a little stuck with the advanced options. If I load a page, it is not of type Event, but if I get - for example - the next page, it is of type Event. It is always the loaded page, that is not of type Event, but of type Page. I think this is a bug in Processwire. To give you a better overview, this is what my events pages look like: Template, test-event and Another Event are all of template event. This is my Events PageType <?php namespace ProcessWire; class Events extends PagesType { public function __construct(ProcessWire $wire, $templates = array(), $parents = array()) { parent::__construct($wire, $templates, $parents); // Make sure we always include the event template and /events/ parent page $this->addTemplates("event"); $this->addParents($this->pages->get("/events/")); $this->setPageClass('Event'); } } As explained in the guide, I also set wire->events: <? // in _init.php $events = new Events($this->wire); $this->wire('events', $events, true); This is the Event page: <?php namespace ProcessWire; class Event extends Page { public function __construct(Template $tpl = null) { if(is_null($tpl)) { $this->template = $this->wire('templates')->get('event'); } if(!$this->parent_id) $this->set('parent_id', $this->wire->pages->get('/events/')->id); parent::__construct($tpl); } Both pages are included and the Event is set as the event templates system pageclass field: Now in the _init.php I debug the $events variable and try to access the methods in the Event class. <? \TD::dump($events->find('template=event')); foreach ($events->find('template=event') as $e) { \TD::dump($e->title); \TD::dump($e); } In the events homepage, this looks like this: So far so good! Now on a subpage, The opened eventpage, is all of a sudden of type Page! I also dump the page object in the event.php template. Do you have any ideas what I'm doing wrong? Thank you a lot in advance! Best, Martin Link to comment Share on other sites More sharing options...
Martin Muzatko Posted September 15, 2017 Author Share Posted September 15, 2017 I found the cause, but I don't know how to fix it. I think the Event.php Page class has to be loaded before PagesLoader checks for wireClassExists($template->pageClass). How should I go about that? If I package it as module, will it be loaded before the check? I am using Composer for my profile/theme and I autoload my pageTypes using psr-4. I guess the check comes before any _init.php is loaded. Somehow, there must be a possibility to hook into the render method to load the custom pagetypes before checking for wireClassExists in the PagesLoader. Link to comment Share on other sites More sharing options...
Martin Muzatko Posted September 15, 2017 Author Share Posted September 15, 2017 I "fixed" it. I created a module that loads the pageTypes for me. <? public function init() { $this->addHookBefore('ProcessPageView::execute', $this, 'loadPageTypes'); } public function loadPageTypes($event) { require_once('site/templates/classes/PageTypes/Event.php'); } This makes the Page, when loaded by ProcessPageView, also available, and thus gets assigned the correct PageClass. Phew! I am not sure this is the best solution. Right now, it is all I have to fix it. Please let me know if there is a better way. What if I want to create a Composer/ProcessWire module with the pagetypes? Link to comment Share on other sites More sharing options...
gebeer Posted January 23, 2020 Share Posted January 23, 2020 On 9/13/2017 at 7:34 PM, Martin Muzatko said: As explained in the guide, I also set wire->events: <? // in _init.php $events = new Events($this->wire); $this->wire('events', $events, true); I know this is an old thread. But just had a similar problem. You added the $events instantiation code to your _init.php which, most likely, is in site/templates/. But this code needs to go in site/init.php as stated in the tutorial. Then everything works as expected. The $page object inside template file site/templates/event.php is of Type Event and not Page. Sometimes it's the little details that matter. Posting this info mainly for my own reference. But maybe someone else might find it useful, too :) 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now