Jump to content

Hook to change title after page is saved


Deyan
 Share

Recommended Posts

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

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.

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