- Where Did That Message Come From?
- Did I Remember All the Language Strings?
- What's Wrong with My SQL Query?
- Using Integers to Stop Hackers
- Nesting Components with Joomla Categories
- Using One-Click Update in Your Extensions
- Conclusion
Nesting Components with Joomla Categories
Joomla 2.5 includes a very powerful built-in category system that supports nesting categories in a hierarchy and setting ACL permissions as the category level. If you're developing a component extension for Joomla, consider using this category system to allow your component items to be grouped by category.
The great thing is that you can do this with just a few lines of code. For example, the core Weblinks component displays the Categories link in the pull-down menu (see Figure 7).
Figure 7 Category Manager menu example in Joomla back end.
The code for this capability actually comes from the installation XML file, in this case administrator/components/com_weblinks/weblinks.xml. Here it is:
<menu link="option=com_categories&extension=com_weblinks" view="categories" img="class:weblinks-cat" alt="Weblinks/Categories">com_weblinks_categories</menu>
Notice the link to com_categories. That's the built-in core component that handles all of the work for categories. All you need to do is to create a link with your extension name in the link—in this case, extension=com_weblinks.
We also want a way to navigate to the category manager screen from the component manager screen. Figure 8 shows how it looks in Weblinks.
Figure 8 Categories tab in Joomla back end.
Here's all of the code for this (from the file administrator/components/com_weblinks/helpers/weblinks.php):
JSubMenuHelper::addEntry( JText::_('COM_WEBLINKS_SUBMENU_CATEGORIES'), 'index.php?option=com_categories&extension=com_weblinks', $vName == 'categories' ); if ($vName=='categories') { JToolBarHelper::title( JText::sprintf('COM_CATEGORIES_CATEGORIES_TITLE', JText::_('com_weblinks')), 'weblinks-categories'); }
This is called from the file display() method of administrator/components/com_weblinks/controller.php, as follows:
// Load the submenu. WeblinksHelper::addSubmenu(JRequest::getCmd('view', 'weblinks'));
That's it! With these few lines of code, we've added all the power of nested categories to the component.