Version 3.0.153 of the core on the dev branch focuses on comments field updates, significant refactoring/improvements to ProcessWire’s core Template class and PagesLoader class (which is used by the $pages API variable).
New $pages API method
The $pages API variable also gains a new method this week:
$pages->has('selector');
This method provides an optimal way to check if any page matching the given selector exists in the system. It can do this with more efficiency than $pages->get();
because it doesn’t have to load any Page, and with more efficiency than $pages->count();
because it doesn’t have to count a total number of matches. However, rather than just returning a true or false, it returns the ID of the first matching page, which adds additional potential use cases.
Previously I had been using $pages->get() or $pages->count() whenever I needed to quickly check for the existence of something. Both are quite fast. But in many cases I didn’t need the loaded Page (object), or the total count, and instead just needed to know if something existed. I’m sure others have had the same need as well. So I thought ProcessWire should have a way of providing this without the overhead of loading a Page or counting a total quantity of pages — the new $pages->has()
method does just that! This method will accept the same $selector argument as any $pages->get(), $pages->find() or $pages->count() method.
In addition to this new method, there was a lot of refactoring and optimization in the PagesLoader class, which is used by the $pages API variable. The updates were significant enough that they belong where they are — on the dev branch — so be sure to let me know if you observe any new issues introduced as a result of the refactoring.
Template updates
The Template class got a significant refactoring that makes code improvements and optimizations to existing methods. In the process, it also gained a few new methods. Most will likely never need or use these, but I did (and a few of you might too), it's good for the core, and it's what I worked on, so I'll mention them anyway. ?
$template->allowNewPages();
Returns an up or down (true or false) as to whether or not new pages are allowed to be created with the given template. It’s basically a simpler interface to the $template->noParents
property.
$template->childTemplates();
This is like the $template->childTemplates
property, except that it returns a WireArray of Template object rather than the IDs (integers) of templates allowed for templates used by child pages.
$template->parentTemplates();
This is like the $template->parentTemplates
property, except that it returns a WireArray of Template objects rather than the IDs (integers) of templates that are allowed for templates used by parent pages.
Either of the methods above can also be used for setting the value when given an argument.
Comments field updates
Finally, the Comments field received major API improvements (also mentioned in last week’s forum post), making the comments field able to better handle completely custom markup for both comments and the forms to submit them. A couple new classes were added (CommentListCustom and CommentFormCustom), and existing classes went through major refactoring. There were some major API additions as well, but none that caused any breaking changes to the existing API, so it should all keep working as it always has.
Both CommentsListCustom and CommentsFormCustom include usage examples at the top of the class files (click the links to see). Use these when you need more control than what the existing comments output provides. Though if you are fine with the existing markup produced by the comments field then using the Custom classes isn’t necessary. But when the need does arise to do something really custom with comments, I think you'll be pleased with what these updates and additions provide.
That’s all for this week. This post is a bit short and updates are a bit technical, I know; thank you for reading anyway. I like to focus on code and details, especially when needing a distraction from the news, as I'm sure we all do. I'm currently adjusting to a new way of working, with everyone home 24/7 for safety; though that part (wife and kids around) is nice, even if it slows work a bit. I hope you and your families are all staying healthy and safe. Have a good weekend and enjoy reading the latest issue of ProcessWire Weekly.