joomlajoomla2.5custom-field-type

Joomla custom field type when editing


how do make it so that when I edit an entry, the correct value for my custom field type is selected? I have this so far:

class JFormFieldCustom extends JFormField {

    protected $type = 'Custom';

    // getLabel() left out

    public function getInput() {

            return '<select id="'.$this->id.'" name="'.$this->name.'">'.
                        '<option value="1" >1</option>'.
                        '<option value="2" >2</option>'.
                    '</select>';
    }

}

How do I pass the selected value to this class so I can do:

<option value="1"SELECTED>1</option> 

or

<option value="2" SELECTED>2</option>

Thanks!


Solution

  • Use $this->value to get selected value.Try this-

     class JFormFieldCustom extends JFormField {
    
            protected $type = 'Custom';
    
            // getLabel() left out
    
            public function getInput() {
    
                    return '<select id="'.$this->id.'" name="'.$this->name.'">'.
                                '<option value="1" <?php echo ($this->value==1)?'selected':''?>>1</option>'.
                                '<option value="2" <?php echo ($this->value==2)?'selected':''?>>2</option>'.
                            '</select>';
            }
        }
    

    Hope this will help.