Jump to content

pageClasses and PHP classes

Recommended Posts

I've been implementing the ideas in the excellent tutorial: https://processwire.com/docs/tutorials/using-custom-page-types-in-processwire/

I really like having a class to mirror certain important templates as it makes organising and structuring code more intuitive (to me) and avoids the need for many hooks. A key aspect of this is the ability to define a Page Class Name for a template, so that any pages with that template will automatically have the correct PHP class. To do this, you need to enable the advanced mode in the site/config.php and then edit the event template in the backend, under the system tab.

However, I have a couple of issues that trouble me:

  1. The system tab carries a warning "Please note that all of these system settings are intended for ProcessWire system development (not site development). Use them at your own risk."  Why is this? I assume that the term  "ProcessWire system development" is intended to mean website apps (I am building a club membership site) as opposed to simple websites, and is not referring to the development of the Processwire system itself, but why the health warning? Is it still beta? What are the risks?
  2. The behaviour is not quite as I expected. If you create a new page with a template which has a pageClass then a hook after "save" shows the following:
    • after entering the new page title and name and saving the "Add New" form, the page content is displayed. The page has presumably been saved as the "after" hook executes and shows that the template and custom pageClass have been assigned to the saved page, but the PHP class is still shown as Page, not the custom class.
    • after the second save (of the page content), the hook shows that the PHP class is correct - the same as the custom pageClass.

Is the behaviour in (2) intentional (and if so, why)? If not, is it a bug, or just unavoidable for some reason? (I also note, BTW, that the page id seems to be 0 after the first "save").

Share this post

Link to post
Share on other sites

The problem behind 2. is probably due to the fact that any core code will still create new pages using new Page(), which will create a Page class and not a pageClass class. So any code running within that request will get a Page. On the next request the page will be loaded from the db and therefore the correct pageClass will be used. 

About 1.:

It's not beta in any ways. E.g. the core User class is using the same system. It's hidden behind the advanced flag simply because you can create a big mess if you're not using it correctly. Imagine you use a pageClass, which does not extend PagesType, but is just some random class. It would probably break quite a lot of things in your website. It's also certainly an option your average developer shouldn't use especially without knowing what those setting do.

"ProcessWire system development" does actually mean what it says. The pageClass option is there because the core uses it for its user pages. Ryan might use it for different pages in the future and if you override it it might clash and break your site. It's quite unlikely (there's been no need for years), but possible.

  • Like 3

Share this post

Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...