Categories
CMS Development Joomla

Custom Category Field

Using Joomla 3.9, although Custom Fields is now available, I am opting to do this in a plugin instead for reasons.

I have created a plugin to hook into the Category view in admin, so that I can add a custom field. This works in terms of, I enable the plugin, and the new tab and my custom field appear in the Edit Category view, but for some reason, the value I set does not save to the params in the database as expected.

Can anybody shed any light on this one?

categoryextension.xml:

<?xml version="1.0" encoding="UTF-8"?>
<form>
    <fields name="params">
        <fieldset name="category-icon" label="Icon">
            <field name="icon" type="text" default="icon-round" label="Category Icon" description="Enter the category icon" size="100" />
        </fieldset>
    </fields>
</form>

categoryextension.php:

<?php
// no direct access
defined('_JEXEC') or die('Restricted access');

class plgContentCategoryExtension extends JPlugin
{

    /**
     * Load the language file on instantiation.
     *
     * @var boolean
     * @since 3.1
     */

    protected $autoloadLanguage = true;

    public function onContentPrepareForm($form, $data)
    {

        $app = JFactory::getApplication();
        $option = $app->input->get('option');
        $view = $app->input->get('view');

        switch ($option) {

            case 'com_categories':

                switch ($view) {

                    case 'category':

                        if ($app->isAdmin()) {

                            JForm::addFormPath(__DIR__ . '/forms');

                            $form->loadFile('categoryextension', false);

                        }

                        return true;
                }

            return true;
        }

        return true;

    }

}

I don’t think there’s anything I ought to do to hook into the save function, as I am using pretty much the same thing for custom fields on com_content for articles, plus the fact the tab and field actually show up in the admin make me believe this is working as expected, but alas, the field value does not save to the db params.

Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *