Jump to content
Deyan

Hook to change title after page is saved

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...