Moo33
Moo33

Reputation: 1283

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!

Upvotes: 0

Views: 1160

Answers (3)

max
max

Reputation: 1

Select for Joomla http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL * @copyright (c) 2017 YouTech Company. All Rights Reserved. * @author macasin */ defined('_JEXEC') or die;

JFormHelper::loadFieldClass('list');

class JFormFieldSelect extends JFormFieldList { protected $type = 'select';

protected function getInput()
{
    $html = array();
    $attr = '';

    // Initialize some field attributes.
    $attr .= !empty($this->class) ? ' class=select ' . $this->class . '"' : ' class=select ';
    $attr .= $this->readonly ? ' readonly' : '';
    $attr .= $this->disabled ? ' disabled' : '';
    $attr .= !empty($this->size) ? ' size="' . $this->size . '"' : '';
    $attr .= $this->required ? ' required aria-required="true"' : '';

    // Initialize JavaScript field attributes.
    $attr .= $this->onchange ? ' onchange="' . $this->onchange . '"' : '';

    // Get the field options.
    $options = $this->getOptions();

    // Load the combobox behavior.
    JHtml::_('behavior.combobox');

    $html[] = '<div class="select input-append">';

    // Build the input for the combo box.
    $html[] = '<select name="' . $this->name . '" id="' . $this->id . '" value="'
        . htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') . '"' . $attr . ' autocomplete="off" >';

    foreach ($options as $option)
    {
        $html[] = '<option '.($option->value == $this->value ? "selected" : "").' value='.$option->value.'>' . $option->text . '</option>';

    }
    $html[] = '</select></div>';

    return implode($html);
}

}

Upvotes: 0

Craig
Craig

Reputation: 9330

It's easier to use what's already there, i.e. extend JFormFieldList in place of JFormField, then all you have to do is return the option's for your list. The inherited functionality will do the rest for you - including selecting the option that matches $this->value

<?php
/**
 * Do the Joomla! security check and get the FormHelper to load the class
 */
defined('_JEXEC') or die('Restricted Access');

JFormHelper::loadFieldClass('list');

class JFormFieldMyCustomField extends JFormFieldList
{
    /**
     * Element name
     *
     * @var     string
     */
    public  $type = 'MyCustomField';

    /**
     * getOptions() provides the options for the select
     *
     * @return  array
     */
    protected function getOptions()
    {
        // Create an array for our options
        $options = array();
        // Add our options to the array
        $options[] = array("value" => 1, "text" => "1);
        $options[] = array("value" => 1, "text" => "1);
        return $options;
    }
}

Upvotes: 4

Irfan
Irfan

Reputation: 7059

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.

Upvotes: 0

Related Questions