Jump to content

Custom page type data initialized only after method called?


Martin Muzatko
 Share

Recommended Posts

Hello!

When I create a custom pagetype by extending the Page class, as explained in the guides, all works good so far.

However, if I want to get data of that page (like its children, etc) the data only seems to be initialized after calling a method.

<?php namespace ProcessWire;

class Event extends Page {

    public $registrations;
    public $items;

	public function __construct(Template $tpl = null) {
		parent::__construct($tpl);
		if(is_null($tpl)) {
			$this->template = $this->wire('templates')->get('event');
		}
		if(!$this->parent_id) $this->set('parent_id', $this->wire->pages->get('/events/'));
      	// is null?
        $this->registrations = $this->get('template=event-registrations');
	}

    public function register()
    {
	    $this->get('template=event-registrations'); // is different from $this->registrations
    }

}

 

So in __construct, there is no access to the page objects data, like title etc. But when I call a function such as $event->register, I get the proper data.
So my question is, is there a way to initialize that data, without retrieving all the data only after I call the method? Or do I need to hook all methods and then do the data initializing?

I think alternatively I could create a getRegistrations() function and call that in every other method where I need the registrations.

Any ideas?

Thank you in advance!

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...