0

I have tring with the same thing but using following code but it not retuns any category what need to pass not getting for the same:

$model_categories = JCategories::getInstance('Thirdparty');
$root = $model_categories->get('root');
$categories = $root->getChildren();
print_r($categories);die; // category items available in db then also returns nothing ..

I need to display category dropdown as same in admin edit article option..

- category 1(optgroup)
-- subcategory 1
-- subcategory 2
- category 2(optgroup)
-- ...

I also have checked link

2 Answers 2

1

Here is query that gets data from category table and display using following code:

there find com_thirdparty and replace with com_your-component-name

$db = JFactory::getDBO();
$query = "SELECT c.id, c.asset_id, c.access, c.alias, c.checked_out, c.checked_out_time,
            c.created_time, c.created_user_id, c.description, c.extension, c.hits, c.language, c.level,
            c.lft, c.metadata, c.metadesc, c.metakey, c.modified_time, c.note, c.params, c.parent_id,
            c.path, c.published, c.rgt, c.title, c.modified_user_id, c.version, CASE WHEN CHAR_LENGTH(c.alias) != 0 THEN CONCAT_WS(':', c.id, c.alias) ELSE c.id END as slug
FROM #__categories as c
LEFT JOIN  (SELECT cat.id as id FROM #__categories AS cat JOIN #__categories AS parent ON cat.lft BETWEEN parent.lft AND parent.rgt WHERE parent.extension = 'com_thirdparty' AND parent.published != 1 GROUP BY cat.id) AS badcats ON badcats.id = c.id
WHERE (c.extension='com_thirdparty' OR c.extension='system') AND c.access IN (1,1,5) AND c.published = 1 AND badcats.id is null
GROUP BY c.id, c.asset_id, c.access, c.alias, c.checked_out, c.checked_out_time,
             c.created_time, c.created_user_id, c.description, c.extension, c.hits, c.language, c.level,
             c.lft, c.metadata, c.metadesc, c.metakey, c.modified_time, c.note, c.params, c.parent_id,
             c.path, c.published, c.rgt, c.title, c.modified_user_id, c.version
ORDER BY c.lft";
$db->setQuery($query);
$catresult = $db->loadObjectList();

<!-- display category -->
<select name="category">
    <option value="">Select Category</option>
    <optgroup>
    <?php
    foreach($catresult as $categoryoption){
        if($categoryoption->level==1){ ?></optgroup><optgroup label="<?php echo $categoryoption->title?>"><?php }
        if($categoryoption->level==2){ ?><option value="<?php echo $categoryoption->id?>">- <?php echo $categoryoption->title?></option><?php }
    }
    ?>
    </optgroup>
</select>
1

If I understand well, you want to display the category select in the edit form or in a list view as a filter ? If so, I guess you use a form for that. Just like the docs say about adding category to the admin.

Developing an MVC Component/Adding categories

The only thing you need to do is to add the Category field in your form xml and then render the field in your view.

In the following, replace the "com_mycom" with your component name and the viewname.

For example : in the components/com_mycom/models/forms/viewname.xml add the following

<field name="category_id" type="category" extension="com_mycom" default=""
       label="COM_MYCOM_FIELD_CATEGORY_LABEL" 
       description="COM_MYCOM_FIELD_CATID_DESC">
</field>

Then in your view components/com_mycom/views/viewname/tmpl/edit.php you have to render the field. example :

<?php echo $this->form->renderField('category_id'); ?>

You have to be more specific about how and where exactly you need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.