How do you modify Category Forms in Joomla?
Asked Answered
T

2

6

I'm working on creating a small plugin to modify the Category Forms (on the add/edit category view) in Joomla.

I was able to use the tutorial on Joomla's site to modify the forms on User Profiles, Articles, and Menus; however, Categories do not seem to work properly.

This the code that I am using:

defined('JPATH_BASE') or die;

class plgContentCategoryType extends JPlugin {

    function onContentPrepareForm($form, $data) {

        // Load plugin language
        $lang = JFactory::getLanguage();
        $lang->load('plg_content_categorytype', JPATH_ADMINISTRATOR);

        if (!($form instanceof JForm)) {
            $this->_subject->setError('JERROR_NOT_A_FORM');
            return false;
        }

        // Check we are manipulating a valid form.
        if (!in_array($form->getName(), array('com_categories.category'))) {
            return true;
        }

        if ($form->getName()=='com_categories.category') {      
            // Add the fields to the form.
            JForm::addFormPath(dirname(__FILE__).'/forms');
            $form->loadFile('categorytype', false);
        }


    }

 }

and this is what the form XML looks like:

<form>
    <fields name="params">

        <fieldset name="categorytype" label="PLG_CONTENT_CATEOGRYTYPE_FIELDSET_LABEL">

            <field name="category_type" type="list" label="PLG_CONTENT_CATEGORYTYPE_LABEL" description="PLG_CONTENT_CATEGORYTYPE_DESC">
                <option value=""></option>
                <option value="features">PLG_CONTENT_CATEGORYTYPE_FEATURES</option>
                <option value="columns">PLG_CONTENT_CATEGORYTYPE_COLUMNS</option>
                <option value="spotlights">PLG_CONTENT_CATEGORYTYPE_SPOTLIGHTS</option>
                <option value="slices">PLG_CONTENT_CATEGORYTYPE_SLICES</option>
                <option value="news">PLG_CONTENT_CATEGORYTYPE_NEWS</option>
            </field>

        </fieldset>

    </fields>
</form>

Any help in what I'm doing wrong would be greatly appreciated! Like I said, it will work on any other type of content, for instance, for it to work on menu's, just have to change the 'name' in the code.

thanks!

Toogood answered 12/8, 2011 at 17:11 Comment(6)
there must be something wrong with loading the file, did you try var_dump($form->getFieldsets()); after $form->loadFile() ?Bagging
var_dump($form->getFieldsets()); doesn't do anything (I put it after loadFile like you suggested). I wanted to make sure that the php file was actually running on the right page, so I intentionally wrote an error and sure enough, when I went to edit a category, it threw the error. So then I went back to having var_dump, and it doesn't do anything.Toogood
and like I said before, all I have to is change "com_categories.category" to "com_menus.item" and it works perfectly, just on a menu and not a category like I want it. so i know the problem isn't the path to the xml file or the xml file itself.Toogood
sorry, i should have mentioned that you have to stop the application to prevent redirects, so use function jexit(); after var_dump(...);Bagging
when I change it to a menu, and add the var_dump, I get a dump which is too much to post here. It's bizarre, I know it's being included on the category page because it will throw an error, but it wont actually run the function. Does onContentPrepareForm not work with Categories?Toogood
Could you grab that var_dump but place echo '<pre>'; before it so it's formatted nicely and paste it into a pastebin service so we can view it? It would help debugging greatlyNila
R
5

Actually there is a Bug in Joomla 2.5 due to which the form fields are not rendered on the Edit Category page. We recently Added a blog on our site which has a fix for this.. You can read it here http://techjoomla.com/joomla-development/adding-custom-fields-to-joomla-categories-in-joomla-25.html

A patch has been submitted for this to Joomla

Reticulation answered 17/10, 2012 at 14:43 Comment(0)
P
2

I set up a testbed and created a similar plugin. I echoed out the value of $form->getName() and it came out as 'com_categories.categorycom_content'

Best guess from this is that as categories can be used in multiple contexts that the component is appended on the end.

So, in the two lines where you have 'com_categories.category', replace with 'com_categories.categorycom_content' and it works.

Polyandrous answered 5/12, 2011 at 1:31 Comment(1)
I would have never found it!. all syntax let you believe to use com_categories.category, great find!Mccorkle

© 2022 - 2024 — McMap. All rights reserved.