Jump to content

Discussion about PageArray->add, PageArray->append

Adam Kiss

Recommended Posts

Hello all,

I wanted to make a pool of pages today. So I created a variable, $pool, and iterated over pages, whose children I wanted to add. All good here:

 $pool = new PageArray;
 foreach ($parentPages as $parent){

However, it took me good 25 minutes to realize, that my code wasn't really working. So, I glanced on docs a little, and changed add for append


To my surprise, neither this works. You must use following:


So, my question to Ryan is: 'why is this like this'? I would guess (and I believe jQuery's add/append/prepend also operate over arrays, not only single items).

And question to community: do you think add/append/prepend/... should work for PageArray also, or only for single Page?

I am all for PageArray. I probably also could do necessary typing. To be precise: Both add() and append() could only call import(), which calls add() for each Page() back, prepend would be a little harder.

Also, trwtf is me for not reading the docs 100%.

Link to comment
Share on other sites

Good idea Adam. Currently it actually will work with a PageArray, but only if that PageArray has just 1 item. That's not intentional, but rather a side effect of the function supporting adding a page by ID number. Since the string value of a PageArray with one item is just a number (the Page ID) it ends up adding that one page. So I think it makes a lot of sense for it to work just like import() if you happen to pass in a PageArray... a definite improvement over what it does now.

I have updated the source for this and will test it locally for a day or two just to make sure there aren't any unintentional side effects. Though I also want to mention that PW isn't trying to mirror jQuery in how it treats one item vs. multiple items--I always prefer to know if I'm dealing with one item or an array of them. So I shy away from mirroring that behavior of jQuery. Even so, I think it does make sense in PW for the add() method to take on the role of import() when you send in multiple items. Thanks for bringing this up.

Link to comment
Share on other sites

No thank you for doing so much for Content Management Systems :)

Anyway, I think this is a matter of preference - you obviously like to know what is code doing by looking at what function you're using. I, on the other hand, prefer the 'here is add method, it will deal with whatever you choose to pass as parameter'. I think it's very nice that you, as a lead developer discuss stuff with community-healthy discussion beat anything else, when it comes to collaboration.

  • Like 2
Link to comment
Share on other sites

I like the way jQuery handles single vs multiple items too, but just not always when translated to PHP and the needs of ProcessWire. There are some language differences between Javascript and PHP, storage differences (in memory vs database), and most importantly big differences between the needs of a CMS vs DOM.

Ultimately having PW treat single/multiple the same as jQuery in all areas would make PW more confusing in places where it makes jQuery more simple. But there are times when it makes a lot of sense, like the one you identified here. And there may be others too, so keep an eye out and let me know. :)

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...