Livusa Posted September 10, 2020 Share Posted September 10, 2020 Hello there, I am fairly new to PW and PHP (spoiler alert: totally new to hooks). I am trying to add a few custom functions to the Admin for certain "patterns" that will be repeating throughout the site I am building. One example of such a pattern is this: I have a 'product' template with a Page Reference field for single page input, 'design'. Once selected, I need to set the title of the product page to the title of this design. I have added the below to the ready.php (pieced together based on whatever I have read up here, feel free to laugh ?). It does the job, but only in 2 saves, as upon the first one it throws a 'Missing required value'-error for the title, although the values do get visibly filled in. Can someone help me out with where I am going wrong here or how else to approach solving this? $this->pages->addHookBefore('saveReady', null, 'buildTitle'); function buildTitle($event) { $page = $event->arguments(0); if($page->template != 'product') return; if($page->template = 'product') { $p = $page->design; $page->title = $p->title; } } Thanks much. Link to comment Share on other sites More sharing options...
kp52 Posted September 10, 2020 Share Posted September 10, 2020 Don't know about the hook, but the PHP "if" condition needs fixing (double equals for comparison operator): if($page->template == 'product') { ? KP Link to comment Share on other sites More sharing options...
dragan Posted September 10, 2020 Share Posted September 10, 2020 You don't need a hook for that. Just render the page title in your frontend template dynamically. Maybe something like this: $pageTitle = $page->title; if($page->template === 'product' && isset($page->design)) { $pageTitle = $page->design->title; } ?> <title><?=$pageTitle?></title> Link to comment Share on other sites More sharing options...
Livusa Posted September 10, 2020 Author Share Posted September 10, 2020 Thanks much, @kp52 and @dragan! To rendering on the front-end: that part is clear. However, I would like to have this behaviour upon creation of new pages in the Admin, if possible. Link to comment Share on other sites More sharing options...
PWaddict Posted September 10, 2020 Share Posted September 10, 2020 I tested this and it works: $this->pages->addHookBefore('saveReady', function($event) { $page = $event->arguments[0]; if($page->template != 'mytemplate') return; if($page->mypagefield) { $p = $page->mypagefield; $page->title = $p->title; } }); Link to comment Share on other sites More sharing options...
Livusa Posted September 14, 2020 Author Share Posted September 14, 2020 Thanks @PWaddict! I have tried your syntax, too, but it still gives me the ‘Missing required field’ for the title when I save it first. ☹️ Can it be some setting I have differently? Or the fact it’s a multilingual page? Just guessing here. I mean, no big deal, I can work around this from the front-end, I just hate that I don’t understand what’s causing this. Link to comment Share on other sites More sharing options...
PWaddict Posted September 14, 2020 Share Posted September 14, 2020 The above hook tested on a site that has 2 languages. Maybe you have another hook that interfere. Empty your site/init.php and site/ready.php and try again. 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