Jump to content

Creating a new page in a hook


Recommended Posts

Hi,

I'm trying to create a simple module, that is hooking into a certain function in Padloper that returns a filled form. I want to use these data to create a new user. But when I try to create a page I'm getting this error:

Call to a member function get() on null (line 30)

Here is my module: 

<?php
class RegisterUser extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => 'Registration during checkout',
            'version' => 1,
            'summary' => 'Register user during checkout',
            'singular' => true,
            'autoload' => true,
            'icon' => 'user',
            );
    }

    public function init() {

        /*

         */
        $this->addHookBefore('PadOrderProcess::saveOrder', $this, 'registerUser');

    }

    /**
     * This is the method that hook run. Since in Padloper we can have any field as pricefield,
     * we need to find out which is the pricefield.
     */
    public function registerUser($event) {
        // This way we can access the arguments that are send to hooked method
        $hp = wire('pages')->get(1);
        $p = new Page();
    	$p->of(false); // sets output formatting to false in order to change field values.
    	$p->parent = $hp;
    	$p->template = "basic-page";
    	$p->title = 'test2'; // sanitized your value from form and sets to title
    	$p->save(); // save the new page

    }
}

This is my first time working with hooks, so maybe I'm missing something. Maybe hooks are strictly for modifying arguments sent to hooked methods? 

Thank you for any help!

Link to comment
Share on other sites

After further investigation it seems that both answers are correct, the problem was something else. For some reason my hook is being fired every time any page is loaded, not just after saveOrder function is being run, as I intended to. For the time being, my workaround is to wrap everything in my registerUser function in an if statement: 

public function registerUser($event) {
        if ($event) {
            $order = $event->arguments('order');
            if ($order->pad_register) {
                ....            
			}
        }
    }

I would love an explanation why is this happening if anyone has one :D

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