RyanJ Posted March 30, 2016 Share Posted March 30, 2016 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 More sharing options...
netcarver Posted March 30, 2016 Share Posted March 30, 2016 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! 3 Link to comment Share on other sites More sharing options...
RyanJ Posted March 31, 2016 Author Share Posted March 31, 2016 Hi Netcarver, I look forward to your solution and thanks for your assistance. Link to comment Share on other sites More sharing options...
netcarver Posted March 31, 2016 Share Posted March 31, 2016 I've PM'd my module over to Ryan for testing. Let's see what comes of it over the next couple of days. If it works out we'll post updates here. 6 Link to comment Share on other sites More sharing options...
netcarver Posted April 1, 2016 Share Posted April 1, 2016 As promised, here's a link to my module that RyanJ's been helping to test out. 5 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now