You can link Joomla Categories functionality in your custom component where you can define a set of categories to use. This way, you don't need separate categories table for your component.

You need to do following things:

  1. You need to link your component with Joomla Categories table through a new field in the database: catid.
  2. You need to add a select box to the edit form (XML Form), so that administrators can add category to your component items.
  3. You need a sub menu link to add and manage categories.
  4. You need to display the category to the list of items.

1. Modify Database Table

You need a field column catid in your database table of items, for which you need to add categories functionality.

catid int(11) NOT NULL DEFAULT 0

Add in the install SQL file or update file accordingly.

2. Edit XML Form

Your items can now belong to a category. So, you need to modify the editing XML form. Add a new category type field.

<field
name="catid"
type="category"
extension="com_helloworld"
class="inputbox"
default=""
label=""
description=""
required="true"
>
<option value="0">JOPTION_SELECT_CATEGORY</option>
</field>

The category can have catid of 0, which represents no category.

3. Add Sub Menu Link

You set the sub menu for categories in the helper file, located in the admin/helpers folder. For the helper file name, use your component name (without com_) or or else your sub menus will not be seen in the category view.

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

4. Display Category in List View

You need to modify the function getListQuery() and use SQL join with the Joomla Categories table to find the associated category title. The name of the category is stored in #__categories table and category ID is stored in table of your component.

// Create the base select statement.
$query->select('a.id as id, a.greeting as greeting, a.published as published')
->from($db->quoteName('#__helloworld', 'a'));

// Join over the categories.
$query->select($db->quoteName('c.title', 'category_title'))
->join('LEFT', $db->quoteName('#__categories', 'c') . ' ON c.id = a.catid');

In your layout template file, you can display category title below the title of the item or as a separate column.

<td>
<a href="/<?php echo $link; ?>" title="<?php echo JText::_('JGLOBAL_EDIT_ITEM'); ?>">
<?php echo $row->title; ?>
</a>
<div class="small">
<?php echo JText::_('JCATEGORY') . ': ' . $this->escape($row->category_title); ?>
</div>
</td>