0

I need to create a custom formfield like so:

https://docs.joomla.org/Creating_a_custom_form_field_type

But the data I want to populate the list with is in a json file. So I can't really do a PHP for each, or if I do, I need to somehow do a JS for each at the same time.

Any advice you can provide to steer me, even to resources would be appreciated.

3
  • 2
    In your code for the custom field can you not just read the json string in from the file and use the php function json_decode to get an associative array which is easy to work with? Commented May 27, 2020 at 20:33
  • You can mark that as the answer @RobbieJackson that was enough to help me on my way. Thanks a million.
    – Eoin
    Commented Jun 13, 2020 at 14:53
  • 1
    Glad it worked ok. I'll leave it to you to include the solution if you like, and mark it correct, as I think you're probably better placed to explain the details. That would be useful to help anyone else who had the same issue. Commented Jun 13, 2020 at 21:03

1 Answer 1

0

Here was the final answer which was placed in mod_modulename/models/fields/stackexchangesites.php

<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

// load the list class
JFormHelper::loadFieldClass('list');

// The class name must always be the same as the filename (in camel case).  
// Extend the list class
class JFormFieldStackexchangesites extends JFormFieldList
{

    //The field class must know its own type through the variable $type.
    protected $type = 'Stackexchangesites';

    // declare the function to get options
    public function getOptions()
    {

        // assign JSON to a variable
        $json = '{"items":[{"aliases":["http://www.stackoverflow.com","http://facebook.stackoverflow.com"],.....~';

        // decode the JSON, and set it as an associative array
        $sites = json_decode($json, true);

        // set the value of i so we can iterate over it
        $i = 0;

        // run a foreach on our JSON to get the values for our options
        foreach($sites['items'] as $site)
        {
            // find the api_site_parameter
            $stackExchangesSitesOptions[] = $site['api_site_parameter'];

            // iterate our i
            $i++;
        }

        // Merge any additional options in the XML definition.
        $options = array_merge(parent::getOptions(), $stackExchangesSitesOptions);
        return $options;
    }
}

Finally, in my XML

..~
    <config>
        <fields name="params">
            // add field path so it knows where to look
            <fieldset name="basic" addfieldpath="/modules/mod_kou_stack_exchange/models/fields">
                ..~
                // include your field
                <field
                        name="stackexchangesites"
                        type="stackexchangesites"
                        label="Stackexchange Sites"
                        description="JFIELD_TITLE_DESC"
                        showon="live_or_beta_stackexchange_sites:live"
                />
                ..~

All the field does is create a dropdown with a list of all the stackexchange sites. The final output of the module loads questions from a stack exchange site based on the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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