Jump to content

Always overwrite field value


suntrop
 Share

Recommended Posts

Hi all 

I have some kind of blog setup and need to change the page date with a custom date. 

Since there is no ‚official‘ way to change the created date, I put in a new date field and I want this field – if not empty – always replaces the created date.

I could use this hook

$this->addHookBefore('Page::render', $this, ‚changeDate');

public function changeDate($event) {
  $page = $event->object;
  if($page->template == 'admin') return;
  if ($page->divergent_date) {
    $page->created = $page->divergent_date;
  }
}

But this only affects the blog article itself. My search page and others seem to »ignore« my overwrite function.

Do you know a better hook or way to overwrite entirely the created field for every use case?

Link to comment
Share on other sites

Hooking does only affect runtime data, but not the database. But lot's of functionality in pw does retrieve pages filtered in the database to keep resource usage to a minimum. You should rather change your template files/selectors to use your custom datefield and add a hook, which does populate your custom field with the creation time if it's empty.

Link to comment
Share on other sites

Thanks, I knew of the SQL »hack«. And I think I’ll have to change it that way. I thought there is some kind of workaround, but as LostKobrakai said there are situations where it doesn’t work well with hooks. I can’t get my head around that limit in PW. Anyway, thanks for the help!!

Link to comment
Share on other sites

Are we talking about a situation where you have some old blog posts that you want to replicate on a new PW installation and you want to keep the old dates? And for some reason you don't want to use a custom date field to store post dates but rather rely on the page created date?

If so it's just a one-time thing to set the created date of old posts so I'm not sure why it matters if the technique seems like a "hack" or not. It's not something you need to use again and again going forward.

But I think using a custom date field for your post dates would be better. It gives you a lot more flexibility. Just set the field to default to today's date and then use this custom date field in any place you would otherwise use the created date.

Link to comment
Share on other sites

I can’t get my head around that limit in PW.

I'd not consider that a limit, but rather you using the field incorrectly. The created (native) field is for determining the date a page was created and it's not by random a system field, because it's used all over processwire's internals. The date you're showing for the blog post is probably no that, but rather a "date the blogpost is supposed to be created for chronological purposes". That's part of your content and therefore rather supposed to be a custom field, especially if you expect it to not align with the page creation date. 

Link to comment
Share on other sites

I'd not consider that a limit, but rather you using the field incorrectly. 

There is some data stored with every page you can’t edit and replicate the same data to edit it. That is weird and makes it only more complicated :-)

Anyhow, don’t want to bother or be disrespectful for all your help!

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