0

I have created a custom form field which extends the list form field.

I takes some JSON and creates the form options. I used this documentation: https://docs.joomla.org/Creating_a_custom_form_field_type

I see here that I can use getValues too, but I really want to setValues.

Updated, working code:

public function getOptions()
{
    $json = '{"items":[{"aliases":["http://www.stackoverflow.com","http://facebook.stackoverflow.com"],"api_site_parameter":"meta.stackoverflow"~}';
    $sites = json_decode($json, true);

    $i = 0;
    foreach($sites['items'] as $site)
    {
        $site = $site['api_site_parameter'];
        $stackExchangesSitesOptions[] = array("value" => $site, "text" => $site);
        $i++;
    }
    // Merge any additional options in the XML definition.
    $options = array_merge(parent::getOptions(), $stackExchangesSitesOptions);
    return $options;
}
7
  • Code Review: 3v4l.org/mc84g ...wow, that documentation page needs some TLC.
    – mickmackusa
    Jun 14 '20 at 13:07
  • 1
    I just updated the documentation page with the learning I just received.
    – Eoin
    Jun 14 '20 at 13:34
  • 1
    ...but you didn't include any of my pieces of advice ...or the change isn't published yet?
    – mickmackusa
    Jun 14 '20 at 13:36
  • Good catch on the '$i', I was using it but no longer needed it. I tried with just text but the option values were 1, 2, 3
    – Eoin
    Jun 14 '20 at 13:36
  • @mickmackusa I have updated it now :) It's still useful as an example in case you DO want different options and text values.
    – Eoin
    Jun 14 '20 at 13:37
1

If you're extending the list form field JFormFieldList then you should provide the function getOptions() which should return the list of options for the select field.

The list of options which you return should be an associative array of name/value pairs like this:

array(array('value' => 1, 'text' => 'option one'),
      array('value' => 2, 'text' => 'option two'),
      array('value' => 3, 'text' => 'option three'),
      );

You will then get the 'value' selected in your http post request from the form.

You can preselect one or more of these values by setting the class protected variable $this->value, but note that to set more than 1 you need to have defined multiple="true" in the XML definition of your form.

See the sample code for City.php in the Sample Component Code section on https://docs.joomla.org/Creating_a_custom_form_field_type

Just for information, getValue() would be used if you had a custom field which extended JFormField. In that case you'd need to provide 3 functions:

  1. getLabel() which would provide the label for the field
  2. getInput() which would provide the html for the input element
  3. getValue() which would provide the value="xxx" attribute for the html input element, and which would then be shown as the value in the input field.
6
  • Thanks for this, I was trying to follow that but I hadn't realised you could use this array to do the list. If you can help me to resolve it I will update the docs with further information. I've updated my question with new code. Can you see what my problem is?
    – Eoin
    Jun 14 '20 at 12:16
  • 1
    I think you need to initialise your variable as an empty array at the start, as in: $stackExchangesSitesOptions = array(); before the loop. Does that work? Jun 14 '20 at 12:25
  • The backend actually works now. I see the values and the name the way I want them and all list options arrive. So that part is correct. But the front end if I echo the formfield I just get the word value. If I do a var_dump I get string(5) "value"
    – Eoin
    Jun 14 '20 at 12:27
  • 1
    It sounds as if it's maybe not picking up your form field code. In your frontend XML file for the form you might have to add an addfieldpath="..." attribute to point to the folder where you have the custom form field php code. Jun 14 '20 at 12:33
  • It was previously picking it up, but it was spitting out 1, 2, 3 as the values. I've added addfieldpath on my <fieldset> already, it wasn't loading in the backend prior to that.
    – Eoin
    Jun 14 '20 at 13:05
1

You can use the setValue method on the form, example:

 $this->form->setValue('name', 'group', 'value');
2
  • Neat, I think for my list formfield I would need to do a foreach to use this method? Or is this only for text inputs?
    – Eoin
    Jun 14 '20 at 12:28
  • Not restricted to text inputs. In your view.html.php, within the display() function, you would typically get the form like $this->form = $this->get('Form'); then use whatever logic you require e.g. if($this->form->getValue() === "perhapsadefaultvalue") { $this->form->setValue('yourfieldname', 'group', 'value'). This probably ought to be done within the getForm() of your model however. To answer your question, for a list formfield, you can use $listfieldvalues[] = $this->form->getValue('yourlistfield') then loop through that array to setValue.
    – Grant G
    Jun 14 '20 at 15:31

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.