Category Item Count: A Possible New Feature?

Written by | 01 June 2015 | Published in 2015 June
How does a new feature end up in Joomla? This is a story about how a new feature is developed and how it might get into a next version of Joomla. From conception, to development, to community feedback & help, to testing, and (hopefully) to adding it to the core code. The story you are about to hear is true; only the names have not been changed to credit the people who helped me out.

“It's a pity that the Joomla Category Manager doesn't have an article counter anymore” Marc Dechèvre told me when we met in the late afternoon. It was the end of the 1st day of Joomladay France 2015 and it was an awesome day: I had met a lot of nice French Joomla people, no major catastrophes had happened during my presentations, and we had another day of Joomladay to go. I understood what Marc meant and heard myself saying, “That should not be that hard to build in...”.

category-item-count-j15Marc had an excellent point there. I had been wondering about the same. Joomla 1.5 had a nice feature in the Category Manager that would list all your website's categories including the number of articles in each category. It would be an excellent overview of the amount of content, how it was divided over the categories & over different statuses.

Joomla's improved Category Manager

There have been some great improvements in the Category Manager since Joomla 1.5: Joomla 1.6 got rid of the Sections, introduced nested categories and added language tags. Joomla 3.x made the whole back-end responsive and changed the UX. The removal of the article count was not an improvement to me, but considering all other improvements, I did not mind after all.

When I arrived at my hotel at the end of the day, it was still before midnight. The 1st day of Joomladay France had been fun & exhausting. The “That should not be that hard to build in...” echo-ed in the back of my mind. I decided to dig into Joomla to try to add a new counter feature to Joomla 3.4.2dev.

menu-item-countJoomla's Menu Manager (Menus > Menu Manager) has a menu item counter that's similar to what I wanted in the Category manager. I looked at the code of HTML output of com_menus and built something similar in the HTML output of com_categories. It did not show any numbers of course, I had not implemented the count in the com_categories “categories” model. When I opened the model, some naughtier word than “Bah” crossed my mind. Adding a counter would be more difficult than expected….

The hidden powers of com_categories

A lot of core components use categories to categorize their items: Banners, Contacts, Newsfeeds, Weblinks. In the past they all had their own code to accomplish their category implementation. When Joomla 1.6 was developed, the com_categories was made generic so that the same code was used for Category Manager for all components. This was done in an elegant way: if you trigger the com_categories component in the back-end using a crafted URL, you can use it to create categories for every component. Let's compare the following URLs:

Article Manager: Articles (via Content > Article Manager) has the URL: 
/administrator/index.php?option=com_content&view=articles
Category Manager: Articles (Content > Category Manager) has the URL: 
/administrator/index.php?option=com_categories&extension=com_content

Now compare with another core component.
Banner Manager: Banners (via Components > Banners > Banners) has the URL: 
/administrator/index.php?option=com_banners
Category Manager: Banners (via Components > Banners > Categories) has the URL: 
/administrator/index.php?option=com_categories&extension=com_banners

Now let's check a core component that does not work with categories:
Redirect Manager: Links (via Components > Redirect) which has the URL: 
/administrator/index.php?option=com_redirect
Let's construct a URL to open com_categories with the extension com_redirect: 
/administrator/index.php?option=com_categories&extension=com_redirect

It triggers the Category Manager which will display “Category Manager: Redirect”. Choose “New” to create a new category and look at the “Category Manager: Add A New Redirect Category”. After “Save & Close” you'll end up with your own new Category for Redirect items! Note that you won't be able to use those categories with your Redirect items because the Redirect Component does not work with & store the category id with its items.

And did I say elegant? Actually, I meant very elegant because the code is constructed in a nice way that you can even use it for 3rd party components. All you need in your component is: a databasefield to store the category (eg catid), a catid field in your components models form .xml, and an addSubmenu method in your helper file to create the left menu (e.g. with items & categories). However, because it’s so generic, I could not add my article counter just by making a small change to the category model...

Back to the Category Items Counter

The 2nd day started with me thinking about the tiny French croissant breakfast I just had. Actually, it got me wondering more about the food differences we have among all countries. The Americans with their cooked breakfast & pancakes in the morning. And who doesn't know the English breakfast of bacon, eggs & beans? I am used to the Dutch breakfast (nothing fancy, just porridge, cereal or thin slices of bread). The Germans have slices of bread too, but you would not make them happy with the Dutch style paper thin slices of meat or cheese (Actually, no-one is happy with that, not even the Dutch, but we just don't know that yet).

Back at the event I went to the Pizza Bugs & Fun room where I explained that the problem was a bit harder than expected / the issues I encountered to Roland Dalmulder and Viktor Vogel. Roland suggested to use a workaround: an “if” statement for “com_content” in the Category Manager. That way I would develop the feature just for com_content. We agreed that it was not the best way to go but at least it would bring me closer to a solution.

I created a new git branch on my local machine, wrote my code and created a Pull Request (PR) at github so that other people could test it & give feedback: https://github.com/joomla/joomla-cms/pull/6916

And feedback I got… (thank you all!). Brian Teeman pointed out that the “Hathor” template uses template overrides. Hathor? Yes, you know, the other administrator non-responsive template that IMHO not many people use. I decided to not do double work but first create an error free version, and later add it to the template overrides of Hathor.

Tobias Zulauf referred to Travis who was not happy with my code. Travis CI is an open-source continuous integration service to test your code with different PHP versions & for code style consistency. Travis was not happy that I used spaces instead of tabs. I had to make a configuration change in my editor PHPStorm so that tabs became real tabs and not spaces.

category-item-count-j3Further feedback I got was about the alignment, its responsive behavior with mobile devices, and the missing column for “archived”. I used the feedback to improve my feature. I even included some other option: I hyperlinked the counters so that you could navigate easily to the Articles, filtering those articles on the category + status that you’ve just clicked.

marc-and-peterMarc Dechèvre (left) & Peter Martin (right)At the end of day 2 of Joomladay France 2015, the new feature was working. Although only working for Articles, I was happy with the result. And Marc too... 

To be continued in next issue: "To a generic solution".

Read 4859 times Tagged under Developers, English
Peter Martin

Peter Martin

Peter Martin is a Joomla Specialist from the Netherlands. His company db8.nl (founded in 2005) offers Joomla consultancy, website implementation, (technical) support and custom Joomla extension development.

Peter is a former Community Leadership Team member. He is currently active inJoomla Forum as Global Moderator, in the Joomla! Bug Squad trying to make small improvements in the Joomla code, as speaker at various conferences and helps to organize Pizza Bugs & Fun in the Netherlands.

In his hometown Nijmegen Peter organizes monthly meetings: Open Coffee Nijmegen (networking meeting for entrepreneurs) and Linux Nijmegen (a Linux User Group).