Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
collection's append() and prepend() behave differently when only one argument is provided #2078
Describe the bug
Note: this issue discusses append() and prepend() methods of collection class, BUT, when following code links from the $page->append() and $page->prepend() reference pages, one leads to 'kirby/src/Cms/Collection.php' while the other leads to 'kirby/src/Toolkit/Collection.php'. I do not know the implications of this, but I assume it is correct, and those are the methods we are using when we do $pages->append($page) or prepend($page)
The methods append() and prepend() from the collection class behave differently when only one argument (for example a page via $pages->append($page)) is provided.
append() checks if the provided element is an object, and if it is, it looks for the object's ID and uses it as key when adding the object to the data array; while prepend() uses array_unshift() effectively adding the element (object or not) without a key, so the key becomes zero (0).
A possible rewrite of prepend() (bear with my low php skills, please) :
Alternatively this difference could be explained in the reference page of the methods. Right now this difference is not visible unless one checks the code, and it already confused other people than myself, see here.
Desktop (please complete the following information):
Your findings regarding the two different implementations in the two different classes are correct. It looks like we implemented a custom
Your possible solution looks pretty good already, but I think I will do some refactoring to always use the