0

I have currently created a Joomla Module, and i would like to make it a bit more advanced now. I have created a Custom field, and i have tried to add it to the mod_NAMEOFMODULE.xml file. To use it when configuring the module. Here is the code for the custom field: `

use Joomla\CMS\Form\FormHelper;

FormHelper::loadFieldClass('list');

class UserFieldList extends JFormFieldList { protected $type = 'UserInfo';

public function getOptions() {
    $cities = array(
        array('value' => 1, 'text' => 'New York'),
        array('value' => 2, 'text' => 'Chicago'),
        array('value' => 3, 'text' => 'San Francisco'),
    );
    // Merge any additional options in the XML definition.
    $options = array_merge(parent::getOptions(), $cities);

    // pre-select values 2 and 3 by setting the protected $value property
    $this->value = array(2, 3);

    return $options;
}

}` It is from the tutorial by joomla: https://docs.joomla.org/Creating_a_custom_form_field_type#Location_of_files

The .xml file code here: <?xml version="1.0" encoding="utf-8"?> <extension type="module" version="3.1.0" client="site" method="upgrade"> <name>Hello, World!</name> <author>John Doe</author> <version>1.0.0</version> <description>A simple Hello, World! module.</description> <files> <filename>mod_helloworld.xml</filename> <filename module="mod_helloworld">mod_helloworld.php</filename> <filename>index.html</filename> <filename>helper.php</filename> <filename>tmpl/default.php</filename> <filename>tmpl/index.html</filename> <filename>UserFieldList.php</filename> <folder>tmpl</folder> </files> <config> <fields name="params"> <fieldset name="basic" addfilepath="/UserFieldList.php"> <field name="userlist" type="UserInfo" label="userlist"> <option>YNO</option> </field> </fieldset> </fields> </config> </extension>. This is taken from the module tutorial by joomla: https://docs.joomla.org/J3.x:Creating_a_simple_module.

As it can be seen in the .xml file, i have been trying to add the filepath in as seen in the custom field tutorial as well as providing the correct type under . The problem is, when i install the module, it only displays a textfield, with the correct name "userlist". It is my understanding, that the custom field is not recognised. Since the textfield is displayed, but not the desired dropdown with 3 options as seen from the UserFieldList class. So is there a way to implement a customfield inside the modules .xml file? Thank you in advance :)

0

With custom fields you need to make sure that (1) the form type in your xml (2) the protected $type variable in your code (3) the class name and (4) the file storing the code all match. So if you have a field type of "UserInfo" then your class name must be JFormFieldUserInfo with protected $type = 'UserInfo'; and the code has to be in a file whose filename is UserInfo.php.

Also for a module Joomla will look in a subdirectory called field for custom field definitions, so if you put your UserFieldList.php file there then you don't need the addfilepath attribute.

Finally, setting the default value to array(2, 3) only works if you have the multiple attribute in your field xml. But because you want to save the field in the database, when you redisplay it you just want the value set to whatever was in the database. So really leave that out and Joomla will set the value based on the database setting.

So in your xml file section include the folder:

<files>
    ...
    <folder>field</folder>
</files>

Put your code for the custom field into field/UserInfo.php (removing setting the value):

<?php
use Joomla\CMS\Form\FormHelper;

FormHelper::loadFieldClass('list');

class JFormFieldUserInfo extends JFormFieldList { 

protected $type = 'UserInfo';

    public function getOptions() {
        $cities = array(
            array('value' => 1, 'text' => 'New York'),
            array('value' => 2, 'text' => 'Chicago'),
            array('value' => 3, 'text' => 'San Francisco'),
        );
        // Merge any additional options in the XML definition.
        $options = array_merge(parent::getOptions(), $cities);

        return $options;
    }
}

This should then work ok.

| improve this answer | |
  • Thank you! That did the job! – jokk May 28 at 9:28

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.