如何从joomla中的自定义表单字段类型中获取保存的值?

时间:2013-01-08 07:29:32

标签: joomla joomla2.5

我为模块创建了自定义表单字段。但是,它会工作但是当我回到模块时,我不知道前一个值或保存的值是什么,因为我没有成功在那里选择。

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

jimport('joomla.form.formfield');

class JFormFieldSlidercategory extends JFormField {

    protected $type = 'Slidercategory';

    // getLabel() left out

    public function getInput() {

                $db = JFactory::getDBO();
                $query = $db->getQuery(true);
                $query->select('id,title');
                $query->from('#__h2mslider_categories');
                $db->setQuery((string)$query);
                $messages = $db->loadObjectList();
                $options ='';


                if ($messages)
                {
                        foreach($messages as $message) 
                        {
                                $options .= '<option value="'.$message->id.'" >'.$message->title.'</option>';
                        }
                }


                $options = '<select id="'.$this->id.'" name="'.$this->name.'">'.
                       '<option value="0" >--select a category--</option>'.
                       $options.
                       '</select>';

                return $options ;


    }
}

我需要能够返回保存值的函数。

1 个答案:

答案 0 :(得分:3)

您可以使用此$this->value

获取价值

或者您可以在选择框中尝试此代码 -

// No direct access to this file
defined('_JEXEC') or die;

// import the list field type
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');

class JFormFieldSlidercategory extends JFormFieldList
{
    /**
     * The field type.
     *
     * @var     string
     */
    protected $type = 'Slidercategory';

    /**
     * Method to get a list of options for a list input.
     *
     * @return  array       An array of JHtml options.
     */
    protected function getOptions() 
    {
        $db = JFactory::getDBO();
            $query = $db->getQuery(true);
            $query->select('id,title');
            $query->from('#__h2mslider_categories');
            $db->setQuery((string)$query);
            $messages = $db->loadObjectList();

        $options = array();
        if ($messages)
        {
            foreach($messages as $message) 
            {
                $options[] = JHtml::_('select.option', $message->id, $message->title);
            }
        }

        $options = array_merge(parent::getOptions(), $options);     
        return $options;
    }
}