Search code examples
phpjoomlacomponentsfieldcategories

Adding category to component in Joomla 3


I use Joomla Component Builder to quick create some small components. Now i creating simple catalog components and time come to add categories, because all other think seems works just fine, but have a problem.

All code for categories was created just fine, i am can add new category and it saves in DB, but did not see any of this cats, when i edit catalog item. I try to find out where problem is and simple made changes in database by adding catid to some items and category shows in list mode, but in edit mode combobox still have only root element.

I check \models\forms\item.xml file and find field description:

<!-- Catid Field. Type: Category. (joomla) -->
<field
    type="category"
    name="catid"
    label="COM_SKYCATALOG_ITEM_CATID_LABEL"
    extension="com_skycatalog.list"
    required="true"
    show_root="true"
    description="COM_SKYCATALOG_ITEM_CATID_DESCRIPTION"
    published="true"
/>

It seems all ok.


Solution

  • It strange that standard way did not work, but i manage it work different way. I just add custom field:

    <?php
    
    // No direct access to this file
    defined('_JEXEC') or die('Restricted access');
    
    JFormHelper::loadFieldClass('list');
    
    /**
     * skycatalog Form Field class for the skycatalog component
     *
     * @since  0.0.1
     */
    class JFormFieldSkyCatalog extends JFormFieldList
    {
        /**
         * The field type.
         *
         * @var         string
         */
        protected $type = 'skycatalog';
    
        /**
         * Method to get a list of options for a list input.
         *
         * @return  array  An array of JHtml options.
         */
        protected function getOptions()
        {
            $db    = JFactory::getDBO();
            $query = $db->getQuery(true);
            $query->select('id, title');
            $query->from('#__categories');
            // Retrieve only published items
            $query->where('#__categories.published = 1','and');
            $query->where("#__categories.extension like 'com_skycatalog.list'",'and');
    
    
            $db->setQuery((string) $query);
            $messages = $db->loadObjectList();
            $options  = array();
    
            if ($messages)
            {
                foreach ($messages as $message)
                {   
                        $options[] = JHtml::_('select.option', $message->id, $message->title);
                }
            }
    
            $options = array_merge(parent::getOptions(), $options);
    
            return $options;
        }
    }
    

    and change field type:

    <field
        type="Skycatalog"
        name="catid"
        class="inputbox"
        label="COM_SKYCATALOG_ITEM_CATID_LABEL"
        extension="com_skycatalog"
        required="true"
        description="COM_SKYCATALOG_ITEM_CATID_DESCRIPTION"
        published="true"
    />
    

    And now it works just fine. Well there are many things to improve, for example, add tree like padding to categories and etc.