nicolant Posted March 10, 2021 Posted March 10, 2021 Please help me to figure out, how to set modified timestamp for PageFiles inside hook. Specifically, I need to mark all files fields in a page and its children as updated on save of that page. $files->modified = time() doesn't work. The only way I have found to modify it is to call $file->install() method on each PageFile. It does the job but duplicates files...
Zeka Posted March 10, 2021 Posted March 10, 2021 @nicolant Have you tried to $file->set('modified', $value);?
nicolant Posted March 10, 2021 Author Posted March 10, 2021 If I change code to: $pp = $page->find(); foreach ($pp as $p) { if ($p->icon && $p->icon->count() > 0) { $p->icon->set("modified",time()); } $p->save(); $cache->deleteFor($p->id); } It gives error: Item 'modified' set to ProcessWire\Pageimages is not an allowed type
BillH Posted March 10, 2021 Posted March 10, 2021 Your code is trying to set a modified time for the field, but you need to set it for each file contained in the field. So replace: $p->icon->set("modified",time()); with foreach($p->icon as $icon) { $icon->set("modified", time()); }
nicolant Posted March 10, 2021 Author Posted March 10, 2021 But when I, for example, upload an image in CMS to that field (set to 1 image only), I can check later $icon->modified and it changed. It can be read on field instead of individual file it contains, why cannot it be overwritten?
BillH Posted March 10, 2021 Posted March 10, 2021 Take a look at the "Formatted value" section of the "Details" tab for your Icon field. You can see that the data can be returned in various forms. So, depending on your settings and the number of images, $icon->modified could return the modified value from an image – but it's not a modified value for the field itself. If you want to set a value for modified, you need to set it for a particular image. Note that if in your code you set output formatting to off (https://cheatsheet.processwire.com/page/built-in-methods-reference/page-setoutputformatting-true-false/) you will always get an array. And it will avoid trouble when setting values. So in your code I'd suggest: ... foreach($pp as $p) { $p->of(false); ...
nicolant Posted March 10, 2021 Author Posted March 10, 2021 Solved following BillH's advise: $p->icon->first()->set("modified", time()); $p->save("icon"); But it started to work only after addition of the last line! Thank you! 1
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