Deyan Posted May 16, 2019 Share Posted May 16, 2019 Hi all, this is my first time to try to use hooks and I'm a bit lost here. What I need is to make a hook that changes title after page is saved. Basically it only adds selected date (field: cal_start_date) to title after page has been published so the end title looks like "2019-05-15 My title". This part works ok (though I think my code is bad). The problem is when I edit page and hit Save button, it changes title to "2019-05-15 2019-05-15 My title" which is not what I want. It should not change title after edits. Here is the code so far: public function init() { $this->pages->addHookAfter('save', $this, 'changeTitle'); } protected function changeTitle($event) { $page = $event->arguments(0); $savedTitle = $page->get('title'); $t = wire()->templates->get($page->template); if($t == 'calender_item' && $page->is(Page::statusUnpublished)) { // if page is unpublished return; } else { $title = $page->get('title'); $date = wireDate('Y-m-d', $page->cal_start_date); $page->of(false); $page->title = $date.'-'.$title; $page->save('title'); $this->message("New saved title is {$page->title}"); } } Any help is welcome. Link to comment Share on other sites More sharing options...
dragan Posted May 16, 2019 Share Posted May 16, 2019 What do you need this for? Is it just for the backend, i.e. what you see in the page tree? If that's the purpose of your hook, you could use custom page list titles. There's a core method for that in advanced mode, and several modules. 1 Link to comment Share on other sites More sharing options...
Deyan Posted May 16, 2019 Author Share Posted May 16, 2019 Aha, thank you very much. I need this only for admin part. I found some old module that does exactly what I need PageListBetterLabels. It trows some errors but I fixed it. Again, thank you & pozdrav. 1 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