Jump to content

How to set 'title' to 'page' selected in Page Reference


Livusa
 Share

Recommended Posts

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

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

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

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