Mansour Alnasser
Mansour Alnasser

Reputation: 5040

How to get the saved value form my custom form field type in joomla?

I have created my custom form field for a module.However, it work will but when ever i get back to the module i don't know what is the previous value or the saved value, because i didn't make it selected there.

<?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 ;


    }
}

I need the function that return me the saved value.

Upvotes: 2

Views: 2964

Answers (1)

Irfan
Irfan

Reputation: 7059

You can get value using this- $this->value

Or you can try this code for select box-

// 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);
        return array_merge(
            parent::getOptions(),
            array_map(
                function ($message) {
                    return JHtml::_('select.option', $message->id, $message->title);
                },
                $db->loadObjectList()
            )
        );
    }
}

Upvotes: 3

Related Questions