Jump to content

[SOLVED] Clear values / Reset page / Replace page


Hani
 Share

Recommended Posts

Is there a quick way to clear all the values in a page through the API instead of looping through all the fields and clearing them one at a time?  I was thinking it would be something along the lines of $page->makeNew().  (I would want to retain the page's name and title, although those can be redefined after clearing all the values.)

If not, is there a way to replace an existing page with a new one and retain the existing page's id, name, and title?

From my understanding of the description for $a->makeNew() in the cheatsheet, that function won't work out of the box for what I intend to do.

$a->makeNew()
Make a new blank WireArray of the same type as $a.
Link to comment
Share on other sites

I figured it out - but only because ProcessWire is so stinkin' logical.

$p = $this->pages->get('/my-page/');
$p->fields = $p->fields->makeNew();
$p->save();

Bonus: it seems to even retain the page's name.  Hope this helps someone out!

Link to comment
Share on other sites

That's cool to know, but you would almost think the function would be called clearold() :)

I guess though makenew() is there to set up new fields for a new page usually before they're populated so I was just trying to be funny above ;)

Link to comment
Share on other sites

That syntax was not intentional. :) I'm glad to hear it works, though I worry if there are any side effects. For example, it seems like $p would still have the original ID of /my-page/. 

Link to comment
Share on other sites

That syntax was not intentional.
 
Is there a another way to do it?  Although, I'm really not worried about sticking with the syntax I have since it seems stable so far.  It doesn't surprise me that it works - ProcessWire seems to be able to handle anything I throw at it!  :)
 
it seems like $p would still have the original ID of /my-page/.
Actually, that was one of the goals I forgot to mention in my original post.  All I really wanted to do was "clear old" values (like Pete alluded to) on a page before redefining the values and re-saving the page.  (I'm saving new field values in a page, but not all the fields will have one - so I wanted to make sure the old values were cleared.)  I've also found that the name/url of the page doesn't get "reset" either - which is great in my case.  Although...I would think I would have to redefine the name of the page if makeNew() cleared all the fields.  Perhaps it has something to do with the name field not being located on the content tab?
Link to comment
Share on other sites

I just noticed that this approach does not clear values from an ASMSelect Page field.  I'll be diving into this to find a solution that works for that input as well.  Will post what I end up with.

Link to comment
Share on other sites

Thanks, Soma! That does the trick to clear ASMSelect fields.  I was hoping to not have to use any of the field names in the module, but since this is a very specific module that won't require scaling, I can live with doing it this way.

Link to comment
Share on other sites

You should be able to iterate through fields without knowing their names - the Cheatsheet should help you there.

Something along the lines of get the page template, iterate through template->fields and then you have access to do something like $page->($field->name)->removeAll();

Just typing that quickly on my phone but hopefully you get the idea.

  • Like 2
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...