suntrop Posted April 20, 2016 Share Posted April 20, 2016 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 More sharing options...
LostKobrakai Posted April 20, 2016 Share Posted April 20, 2016 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 More sharing options...
Robin S Posted April 20, 2016 Share Posted April 20, 2016 This post describes a way of overwriting the created date via the API. 1 Link to comment Share on other sites More sharing options...
suntrop Posted April 21, 2016 Author Share Posted April 21, 2016 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 More sharing options...
Robin S Posted April 21, 2016 Share Posted April 21, 2016 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 More sharing options...
LostKobrakai Posted April 21, 2016 Share Posted April 21, 2016 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 More sharing options...
kongondo Posted April 21, 2016 Share Posted April 21, 2016 Yes, use a custom date field. We do this in the module Blog with an autoload module. Here's the code if you are interested. 2 Link to comment Share on other sites More sharing options...
suntrop Posted April 21, 2016 Author Share Posted April 21, 2016 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 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