Jump to content

Auto Generate page name, title and update field on page creation.


RyanJ
 Share

Recommended Posts

Hello, 

I have modified a module that I found in this topic

To sum it up, I have created a unique field on the root page that is called counter and is set at a unique number. when a new page is created, the module auto populates the page name and page title using this counter. The two functions renderName and CreateTitle work fine.

What I am struggling with is how to update the counter field after the new page is created. Any suggestions or tips are appreciated

class invoiceGenerator extends WireData implements Module {
    /**
     * getModuleInfo is a module required by all modules to tell ProcessWire about them
     *
     * @return array
     *
     */
    private $id;

    public static function getModuleInfo() {

        return array(

            'title' => 'Generates Invoice Number',
            'version' => 100,
            'summary' => 'Set the title field & page name to predefined value',
            'singular' => true,
            'autoload' => true,
            );
    }
    public function init() {
            $this->addHookBefore("Inputfield::render", $this, "renderName");
            $this->pages->addHookBefore('save', $this, 'createTitle');
            $this->addHookAfter('Pages::saved', $this, 'counterUpdate'); 
        }
    //create the page name
    public function renderName(HookEvent $event) {
        // get the current field
        $field = $event->object;
        if(($field->name == 'title' || $field->name == '_pw_page_name') && $field->value == '' ) {
            $counter = wire('pages')->get(1)->invoice_counter;
            $id = "WR-" . date("Y") . "-" . ($counter+1);
            $field->set('value', $id);
            //$this->log("this is page name" . $this->id);
        }
    }
    //create the page title
    public function createTitle(HookEvent $event) {
        $page = $event->arguments[0];
        
        if ($page->template == 'order') {
            $counter = wire('pages')->get(1)->invoice_counter;
            $id = "WR-" . date("Y") . "-" . ($counter+1);
            $page->title = $id;
            //$this->log("this is page name" . $this->id);
        }
    }
    //update the counter field
    public function counterUpdate(HookEvent $event) {
        $page = $event->arguments[0]; 
        $root = wire('pages')->get(1);
        $counter = $root->invoice_counter;
        $root->invoice_counter = $counter++;
        $root->save("invoice_counter"); //save the page          
          
    }
}
Link to comment
Share on other sites

Can't write a long reply at the moment, but your approach is not thread safe (as far as I can tell.) What would happen if another user added some other page between the calls to your two working hook functions?

I have a module that can help. Will post more tomorrow.

Goodnight!

  • Like 3
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...