Jump to content

How to set page file modified time via API?


Recommended Posts

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

Link to post
Share on other sites

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

Link to post
Share on other sites

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());
}

 

Link to post
Share on other sites

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?

Link to post
Share on other sites

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);
    ...
Link to post
Share on other sites

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!

  • Like 1
Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...