Page update with remove old data?

I guess this function only update the fields in the array and leave the rest unchanged?

https://getkirby.com/docs/cheatsheet/page/update

In my case I want to update it and don’t leave anything behind. Is it possible with pure Kirby functions?

Is there a kind of $page->updateAndReplace()? Or maybe it would it be a good idea for a third argument?

$page->update($data = array() [, $lang = null, $cleanup = false]);

Set $cleanup to true to not leave anything behind.

Workaround / solution

This will probably work. Add this before update fields:

f::write($page->textfile(), '')

You can remove fields by passing null as value. But a cleanup flag would probably not be a bad idea at all.

2 Likes

In my case the fields are unknown so I can’t use null as value right away.

What I could do is load the page object first, convert it to array to get the keys and then populate the function with null values. Not as smooth as adding a flag but I can use the Kirby update function and that is probably what I want.

Thanks! :slight_smile:

Could deleting and afterwards re-creating the page be an option, @jenstornell ?

No, then I guess subpages and files are removed as well.

I solved it kind of like this:

It will not work out of the box for every project, needs to be cleaned up, but it can give a hint of what I did.

function merge() {
  return array_merge( $this->keys, $this->revision->content()->toArray() );
}

function update() {
  return $this->page->update( $this->merge );
}

function keys() {
  foreach( $this->page->content()->toArray() as $key => $item ) {
    $keys[$key] = null;
  }
  return $keys;
}
1 Like