简体   繁体   中英

How to use a custom field inside a module in Joomla?

Ok this is not a question, more an answer since i actually didn't find the specific response until i debugged it!

TLDR: always name your class like they should be !

So i am new to Joomla and i personnally hate it but that's on me, i prefer to not use cms when building my website. Anyway I am creating a module since i actually needs logic and i am fed up to stuff everything in JS, then I encountered the weird case where i actually need to make sure the input of the next webmaster is not some garbage. And at that point i said darn it, how could i make a custom field inside my module without creating more? Well the answer is simple, just follow this guide: https://docs.joomla.org/Creating_a_custom_form_field_type right?

Well duh its simple enough, the one thing that is not clear is how to actually use the custom field you made, for sure a quick fix is to move it to libraries\joomla\form\fields but I just forgot something, joomla doesn't know how to register stuff if you name then incorrectly. Take this example:

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

JFormHelper::loadFieldClass('list');

class JFormFieldyear extends JFormFieldList
{
    protected $type = 'Year';

    public function getOptions() {
        $years= array();
        for ($i= date("Y");$i>1800;$i--){
            array_push($years,array('value' => $i, 'text' => $i));
        }
        // Merge any additional options in the XML definition.
        $options = array_merge(parent::getOptions(), $years);
        // preselect last year
        $this->value = array( date("Y"));

        return $options;
    }
}

Do you notice anything wrong? Ok i will give you a hint, the class name is without correct CamelCase. But yeah why does that matter? well introduce Joomla registry by default it will use the filename and Case and prefix it to make sure its the correct class that is loaded.

Ok maybe i was a bit too fast! You need to put your custom field (or forms) in the file name here year.php then you name the class correctly with camel case and the correct prefix and you specify a type which can be whatever because Joomla i guess.

Anyway how to integrate it locally? Well simple enough put your new year.php anywhere in your module, then in your main xml where you are putting your config just do so:

<config>
        <fields name="params">
            <fieldset name="basic">
                <field addfieldpath="/modules/<your_module_name>/<the_folder_chain_where_you_put_year.php>" name="<an_unique_id>" type="year" description="<whatever_maybe_use_an_ini>" label="<whatever_maybe_use_an_ini>" (showon="<other_id>:<other_value>" multiple="multiple")/>
           </fieldset>
       </fields>
</config>

You notice i use the addfieldpath which register the location (but if you didnt do the naming correctly on the class or the filename of the php file then the type will do nothing or default to text) then i put a name (the id) and the type which to be fair could be Year YeAr YeaR (any capitalization). To me this look funny that the registering is so bad on Joomla, i would definetly prefer to register via the type variable inside the custom field and this was case sensitive but yeah i guess this is why using a poorly documented cms is not your go to...

TLDR: Custom fields: https://docs.joomla.org/Creating_a_custom_form_field_type Put the php file in any folder in your module, name it with what type you want in lowercase and name the class inside with JFormField<Name_of_your_file> (upper case on first letter) then in your xml include the path with addfieldpath and specify the type as any casing of the name of your php file.

Cheers,

Neil

TLDR: Custom fields: https://docs.joomla.org/Creating_a_custom_form_field_type Put the php file in any folder in your module, name it with what type you want in lowercase and name the class inside with JFormField<Name_of_your_file> (upper case on first letter) then in your xml include the path with addfieldpath and specify the type as any casing of the name of your php file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM