Jump to content

Creating a new page from a class file fails


Inxentas
 Share

Recommended Posts

I'm writing a class file for an Account page that parses a form whenever a certain 'action' is set in $input->post. The template contains forms to save an existing address, and one to add a new address. Saving the address works great, but my code bugs out the moment I attempt to create a new one. Here is the code I am using in the class file:

<?php namespace ProcessWire;

/**
 * This is the class tied to the page_account template.
 */
class PageAccountPage extends WebshopPage
{
    public $addresses;

    public function __construct($page)
    {
        if($page->input->post->action)
        {
            $id = (int)$page->input->post->id;
            $title = (string)$page->input->post->title;
            $street = (string)$page->input->post->street;
            $zipcode = (string)$page->input->post->zipcode;
            $city = (string)$page->input->post->city;
            $phone = (string)$page->input->post->phone;
            $email = (string)$page->input->post->email;

            switch($page->input->post->action)
            {
                case 'save':
                    // find page
                    $a = $this->pages->get($id);
                    $a->of(false);
                    $a->title = $title;
                    // populate fields
                    $a->webshop_address_street = $street;
                    $a->webshop_address_zipcode = $zipcode;
                    $a->webshop_address_city = $city;
                    $a->webshop_address_phone = $phone;
                    $a->webshop_address_email = $email;
                    // save page
                    $a->save();
                    break;
                case 'add':
                    // create page
                    $new = new Page(); 
                    $new->name = $title;
                    $new->parent = $this->user;
                    $new->template = 'page-address';
                    $new->save();
                    // populate fields
                    $new->title = $title;
                    $new->webshop_address_street = $street;
                    $new->webshop_address_zipcode = $zipcode;
                    $new->webshop_address_city = $city;
                    $new->webshop_address_phone = $phone;
                    $new->webshop_address_email = $email;
                    // save page
                    $new->save();
                    break;
            }
        }

        $this->addresses = $this->user->children("template=page-address");

        parent::__construct($page);
    }
}

The problem sits in the add case of the switch. I get the following error:

Trying to get property 'fieldgroup' of non-object in C:\wamp\www\fransvanberendonk.com\vendor\processwire\processwire\wire\core\PagesEditor.php on line 370

To be clear, the template 'page-address' is one that gets created on module install through code. That code also modifies the User template to allow children in the form of 'page-address' pages. 'page-address' can have a 'user' as a parent. I'm not sure if it's relevant (since it all seems fine from the backend), but here is the code that does that:

// set relationships for page_address
$page_address->parentTemplates([$page_order, $user]);
$page_address->noChildren = 1;
$page_address->save();
// set relationships for user
$user->noChildren = 0;
$children = $user->childTemplates;
$children[] = $page_address;
$user->childTemplates($children);
$user->save();

I also get a message in the CMS after the page bugs out:

PagesLoader: You must assign a template to the page before setting field values (title__data) [pageClass=ProcessWire\Page, template=]

Which a weird message because the pageClass of the page I'm trying to save is PageAccountPage, which inherits from WebshopPage, which interits from Page. I tried not setting the title before the first save() and filling the name value instead, but to no avail. I'm not sure what goes wrong here.

DISCLAIMER: I am aware I should sanitize data first. I wanted to get the thing working first, this code is WIP off course.

Link to comment
Share on other sites

Ah, got the bugger! I was setting the Page template using a string rather then grabbing a full instance of the template. I think this used to work in a previous version or something, but this is way cleaner anyway:

$new->template = $this->templates->get('page-address');
  • 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...