编辑时Joomla自定义字段类型

时间:2013-03-25 04:14:54

标签: joomla joomla2.5 custom-field-type

如何制作,以便在编辑条目时,选择自定义字段类型的正确值?到目前为止我有这个:

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>';
    }

}

如何将所选值传递给此类,以便我可以:

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

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

谢谢!

3 个答案:

答案 0 :(得分:3)

使用已存在的内容更容易,即扩展JFormFieldList代替JFormField,然后您只需返回列表的option's即可。继承的功能将为您完成剩下的工作 - 包括选择与$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;
    }
}

答案 1 :(得分:0)

使用$this->value获取所选值。请尝试此 -

 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>';
        }
    }

希望这会有所帮助。

答案 2 :(得分:0)

选择Joomla http://www.gnu.org/licenses/gpl-2.0.html GNU / GPL  * @copyright(c)2017 YouTech Company。版权所有。  * @author macasin  * / 定义(&#39; _JEXEC&#39;)或死亡;

JFormHelper :: loadFieldClass(&#39;列表&#39);

类JFormFieldSelect扩展了JFormFieldList {     protected $ type =&#39;选择&#39;;

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);
}

}