1

I'm trying to use custom fields on my component. That works fine so far, but I have multiple data structures (tables) in my component, where I want custom fields to work.

Everything works when applying to one table of the component and hard coding the context like default Joomla components do it.

As far as I see in preinstalled Joomla components like "com_content" or "com_contact" custom fields can only be applied to the base row structure of that component.

So how can I apply custom fields to multiple tables if my component uses multiple different database tables to store data, like "customers" and "objects".

I did the helper for my component like this:

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Helper\ContentHelper;
use Joomla\CMS\Component\ComponentHelper;

class MycompHelper extends ContentHelper
{
    public static $extension = 'my_comp';

    public static function addSubmenu($vName)
    {
        // this is done to be able to switch context for different tables
        $type = Factory::getApplication()->input->get('context', 'my_comp');

        if (ComponentHelper::isEnabled('com_fields') && $type) {
            JHtmlSidebar::addEntry(
                Text::_('JGLOBAL_FIELDS'),
                'index.php?option=com_fields&context='.$type,
                $vName == 'fields.fields'
            );
            JHtmlSidebar::addEntry(
                Text::_('JGLOBAL_FIELD_GROUPS'),
                'index.php?option=com_fields&view=groups&context='.$type,
                $vName == 'fields.groups'
            );
        }
    }

    public static function getContexts()
    {
        Factory::getLanguage()->load('my_comp', JPATH_ADMINISTRATOR);

        $contexts = array(
            'my_comp.object'    => Text::_('OBJECT'),
            'my_comp.customer' => Text::_('CUSTOMER')
        );

        return $contexts;
    }
}

It works, but when returning from editing a group, the context is lost. Joomla handles return from field editing different from group editing. Field editing keeps the context in the post, while it is getting lost in group editing and the default value "my_comp" is not enough to work.

So what did i miss, getting custom fields to work with multiple tables of my component. How to set it up, so different tables can use different custom groups and fields?

1 Answer 1

1

I solved it by getting the context from the fields model and using it in the sidebar entries. Custom fields and fieldgroups for every table:

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Helper\ContentHelper;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;

BaseDatabaseModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_fields/models', 'FieldsModel');

class MycompHelper extends ContentHelper
{
    public static $extension = 'my_comp';

    public static function addSubmenu($vName)
    {
        $model = BaseDatabaseModel::getInstance('Fields', 'FieldsModel');
        $context = $model->getState('filter.context');

        if (ComponentHelper::isEnabled('com_fields')) {
            JHtmlSidebar::addEntry(
                Text::_('JGLOBAL_FIELDS'),
                'index.php?option=com_fields&context='. $context,
                $vName == 'fields.fields'
            );
            JHtmlSidebar::addEntry(
                Text::_('JGLOBAL_FIELD_GROUPS'),
                'index.php?option=com_fields&view=groups&context='. $context,
                $vName == 'fields.groups'
            );
        }
    }

    public static function getContexts()
    {
        Factory::getLanguage()->load('my_comp', JPATH_ADMINISTRATOR);

        $contexts = array(
            'my_comp.object'    => Text::_('OBJECT'),
            'my_comp.customer' => Text::_('CUSTOMER')
        );

        return $contexts;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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