How to Add Submenu in Joomla Component

You can a submenu using a helper file. The file is stored in helpers folder with the same name as the name of component name (without com_), or else submenus won't show in category view.

Step 1

admin/helpers/helloworld.php

abstract class HelloWorldHelper extends JHelperContent
{
public static function addSubmenu($submenu)
{
JHtmlSidebar::addEntry('Messages', 'index.php?option=com_helloworld', $submenu == 'helloworlds');

JHtmlSidebar::addEntry('Categories', 'index.php?option=com_categories&view=categories&extension=com_helloworld', $submenu == 'categories');
}
}

The addEntry Function of JHtmlSidebar Class expects three parameters:

  1. Name - string
  2. Link - string (default value is empty)
  3. Active - true or false (default value is false)

You can also use JHtml class loader method like this:

JHtml::_('sidebar.addEntry', 'Messages', 'index.php?option=com_helloworld', $submenu == 'helloworlds');

Step 2

To import the helper class, edit the main entry file of the component, which is admin/helloworld.php file.

admin/helloworld.php

// Require helper file
JLoader::register('HelloWorldHelper', JPATH_COMPONENT . '/helpers/helloworld.php');

Add the code before creating an instance of the controller.

Step 3

Edit the view file to set the sub menu.

admin/views/helloworlds/view.html.php

// Set the submenu
HelloWorldHelper::addSubmenu('helloworlds');

Add the code before setting the toolbar.

Step 4

Update the layout file (default.php) to display the sidebar.

admin/views/helloworlds/tmpl/default.php

<form action .... >
<div id="j-sidebar-container" class="span2">
<?php echo JHtmlSidebar::render(); ?>
</div>
<div id="j-main-container" class="span10">
<div class="row-fluid">
<div class="span6">
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_FILTER'); ?>
<?php echo JLayoutHelper::render('joomla.searchtools.default', array('view' => $this) ); ?>
</div>
</div>
<table class="table table-striped table-hover">

...

</table>
<input type="hidden" name="task" value=""/>
<input type="hidden" name="boxchecked" value="0"/>
<input type="hidden" name="filter_order" value="<?php echo $listOrder; ?>"/>
<input type="hidden" name="filter_order_Dir" value="<?php echo $listDirn; ?>"/>
<?php echo JHtml::_('form.token'); ?>
</div>
</form>