Instead of using a pages method for that, consider using a collection filter. Filters are simpler for that because you don’t have to manually unset items, only return true or false for each item.
Also your current solution will modify the parent collection, not only the returned one, so you would first need to clone $this. That’s all easier with collection filters.
Instead of using a pages method for that, consider using a collection filter. Filters are simpler for that because you don’t have to manually unset items, only return true or false for each item.
I can not use a collection filter here because $item->getStatus() is not a field. It’s a page method.
Also your current solution will modify the parent collection, not only the returned one, so you would first need to clone $this.
If that was true the last foreach would output the same result as the first one, right?
// First one
Projects title-unpublished
New page-unpublished
NotExists-unpublished
Röd-unpublished
Unpublished-unpublished
// Second one
About-private
Projects title-unpublished
Contact-published
Error-published
Error-published
Home-published
New page-unpublished
NotExists-unpublished
Private-private
Röd-unpublished
rwre-private
test-private
Unpublished-unpublished
It does not seem to change the $pages object. My function does what it’s ment. I see no problem with it.
You are right. The $pages object gets cloned before calling the pages method so you don’t have to. However I don’t know if that is actually intended. @distantnative, what do you think?
Hm, that shouldn’t prevent you from using a filter.
I thought that collection filters was made to filter field values, but instead they are made to filter page methods, which in some cases can be a field value.
I tested it and it worked. On just 3 rows I now use “Pages Methods”, “Collection Filter” and “Page Methods”.
Absolutly brilliant!
However I don’t know if that is actually intended. @distantnative, what do you think?
The most correct would probably be to not “reset” the $pages object, but on the other hand, to prevent weird errors for us humans it might be better to leave it like it is.
You can ask yourself this:
What would probably make the most harm in web development?
Are there scenarios of where you don’t want to “reset” the $pages object like this?
I don’t know the answer, but maybe these questions can help you find it?
They work for real Page methods, custom page methods, methods in models and for fields. And in case of other types of collections, it’s just the same for the methods of their children.
Yes. If you don’t need a cloned collection, this can be a performance issue because every clone uses memory, which can be quite much for large collections. So we should only clone if required.
But I see and share your point that cloning is always safer because users can’t overwrite collections by accident.
I think, I actually did not implement the pages methods. They were there for quite some time, just not documented. And I remember that Bastian was a little surprise when I found them as well. Only added page, file and files methods, I believe.
But to be honest, I feel like cloning is rather consistent with methods like visible() and so on that return a cloned and modified version of the pages collection instead of modifying the existing one.
Hm, but the visible method currently clones the collection itself, while the pages methods get a cloned collection without being able to request a non-cloned one. It would definitely be more flexible to pass a non-cloned one, but, as stated previously, cloning is probably easier to understand. So I don’t really know what is better here.
I never had in my mind of the possibility that I could change the template $pages object with it, because I’m outside the template, inside a pages method scope.
My vote goes to keep it like it is now, even if it’s not as flexible and takes up more memory. How much memory are we talking about here?
I haven’t tested it yet, but I believe that it shouldn’t actually be much after thinking about it.
Since the collection is made up of Page objects (which don’t get cloned but just referenced as far as I can tell), the only things that will be duplicated are the references.
This means that the only problem would be that the Page objects are kept around even if they have been deleted, but since PHP’s objects don’t live beyond the current request, they can be cleaned up regularly, so I don’t see a problem here either.