Component Defined Field Type in config.xml [J4]

123 views
Skip to first unread message

Glenn Arkell

unread,
Jun 1, 2021, 3:11:35 AM6/1/21
to Joomla! General Development
I'm trying to create a Custom Field Type for my component as a parameter in config.xml for J4.
I have src/Field/MycustomtypeField.php file which has the following:

use \Joomla\CMS\Form\Field\ListField;

class MycustomtypeField extends ListField
{
    /**
     * The field type.
     */
    protected $type = 'Mycustomtype';

    /**
     * Method to get a list of options for a list input.
     * @return      array           An array of JHtml options.
     */
    protected function getOptions()
    {
        $options = array();

    // then I get the options

        $options = array_merge(parent::getOptions(), $options);

        return $options;
    }
}

And in my config.xml file I have the following fieldset:

    <fieldset label="COM_MYCOMP_TITLE_MULTIUSER"
        name="mycompmultiuser" addfieldprefix="GlennArkell\Component\Mycomp\Administrator\Field"
        description="COM_MYCOMP_TITLE_MULTIUSER_DESC">

         <field name="linkcust_field" type="Mycustomtype" showon="multi_users:1"
             label="COM_MYCOMP_LINKCUST_FIELD_LABEL"
             description="COM_MYCOMP_LINKCUST_FIELD_DESC" />

    </fieldset>

The field displays no problem at all but when I try to save the setting I get the error message:

Fatal error: Cannot declare class MycustomtypeField, because the name is already in use in src/Field/MycustomtypeField.php

All help appreciated.  Cheers.

Roger Creagh

unread,
Jun 1, 2021, 4:38:15 AM6/1/21
to joomla-de...@googlegroups.com
In J3 I do this:

JFormHelper::loadFieldClass('list');

class JFormFieldActors extends JFormFieldList {
    
    protected $type = 'Actors';
    
but maybe J4 is different. Perhaps you don't need the "Field" in the class name.

I did try to install one of my components into J4 recently but it wouldn't even install - no error info available. J4 is a long way from being usable it seems and there is near zero info for migrating existing stuff. The only option seems to be to start again from scratch, which is not going to happen..
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/joomla-dev-general/efa7c5fd-6217-42e3-bc12-2eac0f0f45b6n%40googlegroups.com.

Glenn Arkell

unread,
Jun 1, 2021, 6:19:25 AM6/1/21
to Joomla! General Development
Hi Roger,
Thanks for replying.  Yes J4 is very different when using the new file structure layout.  I walked away from my code and came back to after a break and discovered I had a typo in the file name, how embarrassing.  So it all works as it should now.  I started with a relatively simple component to convert and the biggest part is re-naming files using the latest naming/structure.  I worked through the Clifford's tutorial example MyWalks (https://docs.joomla.org/J4_Component_example_-_Mywalks) which gave me some clues of what to do, but then I experimented a bit to expand functionality.  Anyway, thanks again.  Cheers.

Roger Creagh

unread,
Jun 1, 2021, 1:26:24 PM6/1/21
to joomla-de...@googlegroups.com
Thank you very much for the link to that tutorial Glen - most illuminating. It begs a question for me which I will ask in a separate post.
Roger

Martin Brampton

unread,
Feb 18, 2024, 12:24:10 PMFeb 18
to Joomla! General Development
Thanks for recording this. The file "src/Field/MycustomtypeField.php " is mentioned. Is that src in the site or admin folders?

Glenn Arkell

unread,
Feb 18, 2024, 4:34:47 PMFeb 18
to Joomla! General Development
Hi Martin,
Personally I always store my custom fields in the Admin side (src/Field) and make sure you in the XML form you point to where the field is when using it, ie <form addfieldprefix="GlennArkell\Component\Mycomponent\Administrator\Field">.  Then any field within the form that uses your special field can be found.  Cheers.
Glenn

Martin Brampton

unread,
Feb 19, 2024, 5:23:26 AMFeb 19
to Joomla! General Development
Thanks Glenn. That makes sense. It was a huge struggle to find the requirement for addfieldprefix, so I'm glad to have it recorded here, which is one of the few hits for queries on the topic.

Martin Brampton

unread,
Feb 19, 2024, 12:31:24 PMFeb 19
to Joomla! General Development
I got it kind of working, for use in the options (generated by config.xml). The class extends ListFIeld. I created a list of one or two options in the getOptions method, including code:

        $options = [];
        foreach ($currency_list as $currency) {
            $options[] = array("value" => $currency, "text" => $currency);
        }
        return array_merge(parent::getOptions(), $options);

The options showed up, but there was a large space below them. Any idea what I could have got wrong to cause that?

Steven Berkson

unread,
Feb 19, 2024, 1:35:24 PMFeb 19
to joomla-de...@googlegroups.com

Have you tried:

 

       $options = [];
        foreach ($currency_list as $currency) {

            $options[] = HTMLHelper::_('select.option', $currency, $currency);

      }
        return array_merge(parent::getOptions(), $options);

 

The option function called is in libraries/src/HTML/Helpers/Select.php and among other things trims the text.

 

Otherwise, what is the html output?

--

You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Glenn Arkell

unread,
Feb 19, 2024, 3:29:49 PMFeb 19
to Joomla! General Development
So how are you getting the value of $currency_list?  I have to assume this is from a query on the database?
If my assumption is correct then what I do is have the query in my Helper file and return the objectList and then just merge that with the parent as you are doing and all works fine.  For completeness, I have this as the query in MycomponentHelper file:

public static function getListOptions($table = '#__mycomponent_transactions', $label = 'Transaction', $fld_value = 'id', $fld_text = 'tran_desc' )
{
  // get the records
        $db = Factory::getContainer()->get('DatabaseDriver');
$query = $db->getQuery(true);
        $query->clear();
$query->select(' "0" as "value", " - Select '.$label.' - " as "text" UNION SELECT '. $db->quoteName($fld_value).' as value, '. $db->quoteName($fld_text).' as text ');
$query->from( $db->quoteName($table) );
$query->where( $db->quoteName('state') . ' = 1' );
$query->order(' text ASC ' );
$db->setQuery((string)$query);

    try {
        $options = $db->loadObjectList();
    } catch (RuntimeException $e) {
        Factory::getApplication()->enqueueMessage($e->getMessage(),'danger');
        return false;
    }

return $options;
}

Then in the field file I have:

class TransactionField extends ListField

{
    /**
     * The field type.
     * @var         string
     */
    protected $type = 'Transaction';

/**
 * Name of the layout being used to render the field
 * @var    string
 */
protected $layout = 'joomla.form.field.list';


    /**
     * Method to get a list of options for a list input.
     * @return      array           An array of Html options.
     */
    protected function getOptions()
    {
        $options = MycomponentHelper::getListOptions('#__ mycomponent_transactions ', 'Transaction', 'id', ' tran_desc');

        if (is_null($options)) { $options = array(); }


        $options = array_merge(parent::getOptions(), $options);

        return $options;
    }

}

Hope this helps.

Martin Brampton

unread,
Feb 22, 2024, 11:45:32 AMFeb 22
to Joomla! General Development
Thanks guys. No, it isn't a DB query, it's a call to the Payage API which returns an array of currencies, keys and values both being currency names, such as USD. I did try the HTMLHelper approach, but it didn't make any difference, and I'm inclined to minimise use of Joomla classes where feasible! The display problem was because I specified a size of 50, thinking that would be the width. But for a select list, it is the number of items. Silly me!

Martin Brampton

unread,
Feb 27, 2024, 6:16:59 AMFeb 27
to Joomla! General Development
Anyone know how to make this work in Joomla 3.10.x please?
Reply all
Reply to author
Forward
0 new messages