function toHtml() {
        global $CFG, $PAGE;

        $this->_generateId();
        $elementid = $this->getAttribute('id');
        $mainhtml = html_writer::tag('div', $this->items[$this->getValue()]->mainhtml,
                array('id' => $elementid . '_items_main', 'class' => 'formlistingmain'));

        // Add the main div containing the selected item (+ the caption: "More items").
        $html = html_writer::tag('div', $mainhtml .
                    html_writer::tag('div', $this->showall,
                        array('id' => $elementid . '_items_caption', 'class' => 'formlistingmore')),
                    array('id' => $elementid . '_items', 'class' => 'formlisting hide'));

        // Add collapsible region: all the items.
        $itemrows = '';
        $html .= html_writer::tag('div', $itemrows,
                array('id' => $elementid . '_items_all', 'class' => 'formlistingall'));

        // Add radio buttons for non javascript support.
        $radiobuttons = '';
        foreach ($this->items as $itemid => $item) {
            $radioparams = array('name' => $this->getName(), 'value' => $itemid,
                    'id' => 'id_'.$itemid, 'class' => 'formlistinginputradio', 'type' => 'radio');
            if ($itemid == $this->getValue()) {
                $radioparams['checked'] = 'checked';
            }
            $radiobuttons .= html_writer::tag('div', html_writer::tag('input',
                html_writer::tag('div', $item->rowhtml, array('class' => 'formlistingradiocontent')), $radioparams),
                array('class' => 'formlistingradio'));
        }

        // Container for the hidden hidden input which will contain the selected item.
        $html .= html_writer::tag('div', $radiobuttons,
                array('id' => 'formlistinginputcontainer_' . $elementid, 'class' => 'formlistinginputcontainer'));

        $module = array('name'=>'form_listing', 'fullpath'=>'/lib/form/yui/listing/listing.js',
            'requires'=>array('node', 'event', 'transition', 'escape'));

        $PAGE->requires->js_init_call('M.form_listing.init',
                 array(array(
                'elementid' => $elementid.'_items',
                'hideall' => $this->hideall,
                'showall' => $this->showall,
                'hiddeninputid' => $this->getAttribute('id'),
                'items' => $this->items,
                'inputname' => $elementid,
                'currentvalue' => $this->getValue())), true, $module);

        return $html;
    }