Jump to content

Custom Page is not of type when loaded, but others are.


Martin Muzatko
 Share

Recommended Posts

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:

  32Q_mg2ivz0pQAsm-TmMLKUz0Z4jbAeSy-vdO-yufas?size=800x600&size_mode=3 

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:

 

X95KnmniA29AVCDZ2Q1qX3nMhenS_GoZwRlQdTbF

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:

image.thumb.png.f7a320cb5b662b990f9a29816020357b.png

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.

image.thumb.png.d71a54eed7de33f9e704ff167f5f8790.png

Do you have any ideas what I'm doing wrong?

 

Thank you a lot in advance!

Best,
Martin

image.png

image.png

Link to comment
Share on other sites

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

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

  • 2 years later...
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 :)

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...