1

I need help in adding extra fields to Joomla categories(not article) in my custom component categories and I didnt find any documentation for that. I want to add a time field to categories. I tried to use fieldsattach but it is giving lots of errors. So I started building my own plugin following some joomla docs but i am not able to add extra fields into categories. This is what I tried till now. I created a plugin xml file named plg_bfquiz_catextras.xml

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.1" type="plugin" group="categories">
    <name>plg_bfquiz_catextras</name>
    <author>Joomla! Project</author>
    <creationDate>January 2021</creationDate>
    <copyright>Copyright (C) 2021 - 2022 . All rights reserved.</copyright>
    <license>GNU General Public License version 2 or later; see LICENSE.txt</license>
    <authorEmail></authorEmail>
    <authorUrl></authorUrl>
    <version>3.1.0</version>
    <description>PLG_BFQUIZ_CATEXTRAS_XML_DESCRIPTION</description>
    <files>
        <filename plugin="plg_bfquiz_catextras">plg_bfquiz_catextras.php</filename>
        <folder>forms</folder>
        <filename>index.html</filename>
    </files>
    <languages>
        <language tag="en-GB">en-GB.plg_bfquiz_catextras.ini</language>
        <language tag="en-GB">en-GB.plg_bfquiz_catextras.sys.ini</language>
    </languages>
</extension>

Then in plugin plg_bfquiz_catextras.php file

<?php
// no direct access
defined ('_JEXEC') or die;
class plgCategoriesBFQuizCatextras extends JPlugin
{
    /**
     * Load the language file on instantiation.
     * Note this is only available in Joomla 3.1 and higher.
     * If you want to support 3.0 series you must override the constructor
     *
     * @var boolean
     * @since <your version>
     */
    protected $autoloadLanguage = true;

    /**
     * Prepare form and add my field.
     *
     * @param   JForm  $form  The form to be altered.
     * @param   mixed  $data  The associated data for the form.
     *
     * @return  boolean
     *
     * @since   <your version>
     */
    function onContentPrepareForm($form, $data)
    {
        $app    = JFactory::getApplication();
        $option = $app->input->get('option');

        switch($option)
        {
            case 'com_bfquiz' :
                if ($app->isClient('administrator'))
                {
                    JForm::addFormPath(__DIR__ . '/forms');
                    $form->loadFile('catxtras', false);
                }

                return true;
        }

        return true;
    }
}
?>

And finally I have a forms folder inside which there is a file named catextras.xml with content

<?xml version="1.0" encoding="UTF-8"?>
<form>
    <fields name="params">
        <fieldset name="params" label="PLG_BFQUIZ_CATEXTRAS_FIELDSET_LABEL">
            <field
                name="cat_time"
                type="integer"
                label="PLG_BFQUIZ_CATEXTRAS_TIME"
            />
        </fieldset>
    </fields>
</form>

I know I am doing some mistake but I am unable to point it out. Need help from Joomla Experts.

1 Answer 1

1

There are many issues with your plugin. First, it doesn't follow the naming convention so your class doesn't get detected at all. See Creating a Plugin for Joomla tutorial for an example of correct class naming.

Assuming your component is using com_categories to manage categories, the plugin is using categories plugin group but it should be using content group instead. The context check is also wrong, it should be using name of the form ($form->getName()) instead of current component from input (which would be com_categories). You probably also have a typo. In your post you refer to the form file as catextras but in the code it's called catxtras.

If your component does use com_categories for categories and you want custom fields to be available at all times, you don't even need a plugin. Instead create category.xml file in administrator/components/com_[yourcomponent]/models/forms and it will be loaded automatically. If you need more control over the form, you can create a helper class [yourcomponent]HelperCategory in administrator/components/com_[yourcomponent]/helpers/category.php with onPrepareForm() method.

1
  • Thanks. Actually i searched for prepareform and came across category model. There i found that category.php file should be there inside my helpers folder. There on i was able to create a custom field. Some 3 hours it took as i could not find any doc by googling.
    – Amit Ray
    Jan 31, 2021 at 17:17

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.